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 /proc/rtc
9 * 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.
38 #define RTC_VERSION "1.09"
40 #define RTC_IRQ 8 /* Can't see this changing soon. */
41 #define RTC_IO_EXTENT 0x10 /* Only really two ports, but... */
44 * Note that *all* calls to CMOS_READ and CMOS_WRITE are done with
45 * interrupts disabled. Due to the index-port/data-port (0x70/0x71)
46 * design of the RTC, we don't want two different things trying to
47 * get to it at once. (e.g. the periodic 11 min sync from time.c vs.
51 #include <linux/types.h>
52 #include <linux/errno.h>
53 #include <linux/miscdevice.h>
54 #include <linux/malloc.h>
55 #include <linux/ioport.h>
56 #include <linux/fcntl.h>
57 #include <linux/mc146818rtc.h>
58 #include <linux/init.h>
59 #include <linux/poll.h>
62 #include <asm/uaccess.h>
63 #include <asm/system.h>
66 * We sponge a minor off of the misc major. No need slurping
67 * up another valuable major dev number for this. If you add
68 * an ioctl, make sure you don't conflict with SPARC's RTC
72 static struct wait_queue
*rtc_wait
;
74 static struct timer_list rtc_irq_timer
;
76 static long long rtc_llseek(struct file
*file
, loff_t offset
, int origin
);
78 static ssize_t
rtc_read(struct file
*file
, char *buf
,
79 size_t count
, loff_t
*ppos
);
81 static int rtc_ioctl(struct inode
*inode
, struct file
*file
,
82 unsigned int cmd
, unsigned long arg
);
84 static unsigned int rtc_poll(struct file
*file
, poll_table
*wait
);
86 void get_rtc_time (struct rtc_time
*rtc_tm
);
87 void get_rtc_alm_time (struct rtc_time
*alm_tm
);
88 void rtc_dropped_irq(unsigned long data
);
90 void set_rtc_irq_bit(unsigned char bit
);
91 void mask_rtc_irq_bit(unsigned char bit
);
93 static inline unsigned char rtc_is_updating(void);
96 * Bits in rtc_status. (7 bits of room for future expansion)
99 #define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
100 #define RTC_TIMER_ON 0x02 /* missed irq timer active */
102 unsigned char rtc_status
= 0; /* bitmapped status byte. */
103 unsigned long rtc_freq
= 0; /* Current periodic IRQ rate */
104 unsigned long rtc_irq_data
= 0; /* our output to the world */
107 * If this driver ever becomes modularised, it will be really nice
108 * to make the epoch retain its value across module reload...
111 static unsigned long epoch
= 1900; /* year corresponding to 0x00 */
113 unsigned char days_in_mo
[] =
114 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
117 * A very tiny interrupt handler. It runs with SA_INTERRUPT set,
118 * so that there is no possibility of conflicting with the
119 * set_rtc_mmss() call that happens during some timer interrupts.
120 * (See ./arch/XXXX/kernel/time.c for the set_rtc_mmss() function.)
123 static void rtc_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
126 * Can be an alarm interrupt, update complete interrupt,
127 * or a periodic interrupt. We store the status in the
128 * low byte and the number of interrupts received since
129 * the last read in the remainder of rtc_irq_data.
132 rtc_irq_data
+= 0x100;
133 rtc_irq_data
&= ~0xff;
134 rtc_irq_data
|= (CMOS_READ(RTC_INTR_FLAGS
) & 0xF0);
135 wake_up_interruptible(&rtc_wait
);
137 if (rtc_status
& RTC_TIMER_ON
)
138 mod_timer(&rtc_irq_timer
, jiffies
+ HZ
/rtc_freq
+ 2*HZ
/100);
142 * Now all the various file operations that we export.
145 static long long rtc_llseek(struct file
*file
, loff_t offset
, int origin
)
150 static ssize_t
rtc_read(struct file
*file
, char *buf
,
151 size_t count
, loff_t
*ppos
)
153 struct wait_queue wait
= { current
, NULL
};
157 if (count
< sizeof(unsigned long))
160 add_wait_queue(&rtc_wait
, &wait
);
162 current
->state
= TASK_INTERRUPTIBLE
;
164 while ((data
= xchg(&rtc_irq_data
, 0)) == 0) {
165 if (file
->f_flags
& O_NONBLOCK
) {
169 if (signal_pending(current
)) {
170 retval
= -ERESTARTSYS
;
176 retval
= put_user(data
, (unsigned long *)buf
);
178 retval
= sizeof(unsigned long);
180 current
->state
= TASK_RUNNING
;
181 remove_wait_queue(&rtc_wait
, &wait
);
186 static int rtc_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
,
191 struct rtc_time wtime
;
194 case RTC_AIE_OFF
: /* Mask alarm int. enab. bit */
196 mask_rtc_irq_bit(RTC_AIE
);
199 case RTC_AIE_ON
: /* Allow alarm interrupts. */
201 set_rtc_irq_bit(RTC_AIE
);
204 case RTC_PIE_OFF
: /* Mask periodic int. enab. bit */
206 mask_rtc_irq_bit(RTC_PIE
);
207 if (rtc_status
& RTC_TIMER_ON
) {
208 del_timer(&rtc_irq_timer
);
209 rtc_status
&= ~RTC_TIMER_ON
;
213 case RTC_PIE_ON
: /* Allow periodic ints */
217 * We don't really want Joe User enabling more
218 * than 64Hz of interrupts on a multi-user machine.
220 if ((rtc_freq
> 64) && (!capable(CAP_SYS_RESOURCE
)))
223 if (!(rtc_status
& RTC_TIMER_ON
)) {
224 rtc_status
|= RTC_TIMER_ON
;
225 rtc_irq_timer
.expires
= jiffies
+ HZ
/rtc_freq
+ 2*HZ
/100;
226 add_timer(&rtc_irq_timer
);
228 set_rtc_irq_bit(RTC_PIE
);
231 case RTC_UIE_OFF
: /* Mask ints from RTC updates. */
233 mask_rtc_irq_bit(RTC_UIE
);
236 case RTC_UIE_ON
: /* Allow ints for RTC updates. */
238 set_rtc_irq_bit(RTC_UIE
);
241 case RTC_ALM_READ
: /* Read the present alarm time */
244 * This returns a struct rtc_time. Reading >= 0xc0
245 * means "don't care" or "match all". Only the tm_hour,
246 * tm_min, and tm_sec values are filled in.
249 get_rtc_alm_time(&wtime
);
252 case RTC_ALM_SET
: /* Store a time into the alarm */
255 * This expects a struct rtc_time. Writing 0xff means
256 * "don't care" or "match all". Only the tm_hour,
257 * tm_min and tm_sec are used.
259 unsigned char hrs
, min
, sec
;
260 struct rtc_time alm_tm
;
262 if (copy_from_user(&alm_tm
, (struct rtc_time
*)arg
,
263 sizeof(struct rtc_time
)))
266 hrs
= alm_tm
.tm_hour
;
281 if (!(CMOS_READ(RTC_CONTROL
) & RTC_DM_BINARY
) ||
288 CMOS_WRITE(hrs
, RTC_HOURS_ALARM
);
289 CMOS_WRITE(min
, RTC_MINUTES_ALARM
);
290 CMOS_WRITE(sec
, RTC_SECONDS_ALARM
);
291 restore_flags(flags
);
295 case RTC_RD_TIME
: /* Read the time/date from RTC */
297 get_rtc_time(&wtime
);
300 case RTC_SET_TIME
: /* Set the RTC */
302 struct rtc_time rtc_tm
;
303 unsigned char mon
, day
, hrs
, min
, sec
, leap_yr
;
304 unsigned char save_control
, save_freq_select
;
308 if (!capable(CAP_SYS_TIME
))
311 if (copy_from_user(&rtc_tm
, (struct rtc_time
*)arg
,
312 sizeof(struct rtc_time
)))
315 yrs
= rtc_tm
.tm_year
+ 1900;
316 mon
= rtc_tm
.tm_mon
+ 1; /* tm_mon starts at zero */
317 day
= rtc_tm
.tm_mday
;
318 hrs
= rtc_tm
.tm_hour
;
325 leap_yr
= ((!(yrs
% 4) && (yrs
% 100)) || !(yrs
% 400));
327 if ((mon
> 12) || (day
== 0))
330 if (day
> (days_in_mo
[mon
] + ((mon
== 2) && leap_yr
)))
333 if ((hrs
>= 24) || (min
>= 60) || (sec
>= 60))
336 if ((yrs
-= epoch
) > 255) /* They are unsigned */
341 if (!(CMOS_READ(RTC_CONTROL
) & RTC_DM_BINARY
)
344 restore_flags(flags
);
358 save_control
= CMOS_READ(RTC_CONTROL
);
359 CMOS_WRITE((save_control
|RTC_SET
), RTC_CONTROL
);
360 save_freq_select
= CMOS_READ(RTC_FREQ_SELECT
);
361 CMOS_WRITE((save_freq_select
|RTC_DIV_RESET2
), RTC_FREQ_SELECT
);
363 CMOS_WRITE(yrs
, RTC_YEAR
);
364 CMOS_WRITE(mon
, RTC_MONTH
);
365 CMOS_WRITE(day
, RTC_DAY_OF_MONTH
);
366 CMOS_WRITE(hrs
, RTC_HOURS
);
367 CMOS_WRITE(min
, RTC_MINUTES
);
368 CMOS_WRITE(sec
, RTC_SECONDS
);
370 CMOS_WRITE(save_control
, RTC_CONTROL
);
371 CMOS_WRITE(save_freq_select
, RTC_FREQ_SELECT
);
373 restore_flags(flags
);
376 case RTC_IRQP_READ
: /* Read the periodic IRQ rate. */
378 return put_user(rtc_freq
, (unsigned long *)arg
);
380 case RTC_IRQP_SET
: /* Set periodic IRQ rate. */
386 * The max we can do is 8192Hz.
388 if ((arg
< 2) || (arg
> 8192))
391 * We don't really want Joe User generating more
392 * than 64Hz of interrupts on a multi-user machine.
394 if ((arg
> 64) && (!capable(CAP_SYS_RESOURCE
)))
397 while (arg
> (1<<tmp
))
401 * Check that the input was really a power of 2.
410 val
= CMOS_READ(RTC_FREQ_SELECT
) & 0xf0;
412 CMOS_WRITE(val
, RTC_FREQ_SELECT
);
413 restore_flags(flags
);
417 case RTC_EPOCH_READ
: /* Read the epoch. */
419 return put_user (epoch
, (unsigned long *)arg
);
421 case RTC_EPOCH_SET
: /* Set the epoch. */
424 * There were no RTC clocks before 1900.
429 if (!capable(CAP_SYS_TIME
))
439 return copy_to_user((void *)arg
, &wtime
, sizeof wtime
) ? -EFAULT
: 0;
443 * We enforce only one user at a time here with the open/close.
444 * Also clear the previous interrupt data on an open, and clean
445 * up things on a close.
448 static int rtc_open(struct inode
*inode
, struct file
*file
)
450 if(rtc_status
& RTC_IS_OPEN
)
453 rtc_status
|= RTC_IS_OPEN
;
458 static int rtc_release(struct inode
*inode
, struct file
*file
)
461 * Turn off all interrupts once the device is no longer
462 * in use, and clear the data.
470 tmp
= CMOS_READ(RTC_CONTROL
);
474 CMOS_WRITE(tmp
, RTC_CONTROL
);
475 CMOS_READ(RTC_INTR_FLAGS
);
476 restore_flags(flags
);
478 if (rtc_status
& RTC_TIMER_ON
) {
479 rtc_status
&= ~RTC_TIMER_ON
;
480 del_timer(&rtc_irq_timer
);
484 rtc_status
&= ~RTC_IS_OPEN
;
488 static unsigned int rtc_poll(struct file
*file
, poll_table
*wait
)
490 poll_wait(file
, &rtc_wait
, wait
);
491 if (rtc_irq_data
!= 0)
492 return POLLIN
| POLLRDNORM
;
497 * The various file operations we support.
500 static struct file_operations rtc_fops
= {
504 NULL
, /* No readdir */
513 static struct miscdevice rtc_dev
=
520 __initfunc(int rtc_init(void))
524 unsigned int year
, ctrl
;
525 unsigned long uip_watchdog
;
528 printk(KERN_INFO
"Real Time Clock Driver v%s\n", RTC_VERSION
);
529 if(request_irq(RTC_IRQ
, rtc_interrupt
, SA_INTERRUPT
, "rtc", NULL
))
531 /* Yeah right, seeing as irq 8 doesn't even hit the bus. */
532 printk(KERN_ERR
"rtc: IRQ %d is not free.\n", RTC_IRQ
);
535 misc_register(&rtc_dev
);
536 /* Check region? Naaah! Just snarf it up. */
537 request_region(RTC_PORT(0), RTC_IO_EXTENT
, "rtc");
541 /* Each operating system on an Alpha uses its own epoch.
542 Let's try to guess which one we are using now. */
544 uip_watchdog
= jiffies
;
545 if (rtc_is_updating() != 0)
546 while (jiffies
- uip_watchdog
< 2*HZ
/100)
551 year
= CMOS_READ(RTC_YEAR
);
552 ctrl
= CMOS_READ(RTC_CONTROL
);
553 restore_flags(flags
);
555 if (!(ctrl
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
)
556 BCD_TO_BIN(year
); /* This should never happen... */
558 if (year
> 10 && year
< 44) {
560 guess
= "ARC console";
561 } else if (year
< 96) {
563 guess
= "Digital UNIX";
566 printk("rtc: %s epoch (%lu) detected\n", guess
, epoch
);
568 init_timer(&rtc_irq_timer
);
569 rtc_irq_timer
.function
= rtc_dropped_irq
;
573 /* Initialize periodic freq. to CMOS reset default, which is 1024Hz */
574 CMOS_WRITE(((CMOS_READ(RTC_FREQ_SELECT
) & 0xF0) | 0x06), RTC_FREQ_SELECT
);
575 restore_flags(flags
);
581 * At IRQ rates >= 4096Hz, an interrupt may get lost altogether.
582 * (usually during an IDE disk interrupt, with IRQ unmasking off)
583 * Since the interrupt handler doesn't get called, the IRQ status
584 * byte doesn't get read, and the RTC stops generating interrupts.
585 * A timer is set, and will call this function if/when that happens.
586 * To get it out of this stalled state, we just read the status.
587 * At least a jiffy of interrupts (rtc_freq/HZ) will have been lost.
588 * (You *really* shouldn't be trying to use a non-realtime system
589 * for something that requires a steady > 1KHz signal anyways.)
592 void rtc_dropped_irq(unsigned long data
)
596 printk(KERN_INFO
"rtc: lost some interrupts at %ldHz.\n", rtc_freq
);
597 mod_timer(&rtc_irq_timer
, jiffies
+ HZ
/rtc_freq
+ 2*HZ
/100);
601 rtc_irq_data
+= ((rtc_freq
/HZ
)<<8);
602 rtc_irq_data
&= ~0xff;
603 rtc_irq_data
|= (CMOS_READ(RTC_INTR_FLAGS
) & 0xF0); /* restart */
604 restore_flags(flags
);
608 * Info exported via "/proc/rtc".
611 int get_rtc_status(char *buf
)
615 unsigned char batt
, ctrl
;
620 batt
= CMOS_READ(RTC_VALID
) & RTC_VRT
;
621 ctrl
= CMOS_READ(RTC_CONTROL
);
622 restore_flags(flags
);
629 * There is no way to tell if the luser has the RTC set for local
630 * time or for Universal Standard Time (GMT). Probably local though.
633 "rtc_time\t: %02d:%02d:%02d\n"
634 "rtc_date\t: %04d-%02d-%02d\n"
635 "rtc_epoch\t: %04lu\n",
636 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
,
637 tm
.tm_year
+ 1900, tm
.tm_mon
+ 1, tm
.tm_mday
, epoch
);
639 get_rtc_alm_time(&tm
);
642 * We implicitly assume 24hr mode here. Alarm values >= 0xc0 will
643 * match any value for that particular field. Values that are
644 * greater than a valid time, but less than 0xc0 shouldn't appear.
646 p
+= sprintf(p
, "alarm\t\t: ");
647 if (tm
.tm_hour
<= 24)
648 p
+= sprintf(p
, "%02d:", tm
.tm_hour
);
650 p
+= sprintf(p
, "**:");
653 p
+= sprintf(p
, "%02d:", tm
.tm_min
);
655 p
+= sprintf(p
, "**:");
658 p
+= sprintf(p
, "%02d\n", tm
.tm_sec
);
660 p
+= sprintf(p
, "**\n");
666 "square_wave\t: %s\n"
669 "periodic_IRQ\t: %s\n"
670 "periodic_freq\t: %ld\n"
671 "batt_status\t: %s\n",
672 (ctrl
& RTC_DST_EN
) ? "yes" : "no",
673 (ctrl
& RTC_DM_BINARY
) ? "no" : "yes",
674 (ctrl
& RTC_24H
) ? "yes" : "no",
675 (ctrl
& RTC_SQWE
) ? "yes" : "no",
676 (ctrl
& RTC_AIE
) ? "yes" : "no",
677 (ctrl
& RTC_UIE
) ? "yes" : "no",
678 (ctrl
& RTC_PIE
) ? "yes" : "no",
680 batt
? "okay" : "dead");
686 * Returns true if a clock update is in progress
688 static inline unsigned char rtc_is_updating(void)
695 uip
= (CMOS_READ(RTC_FREQ_SELECT
) & RTC_UIP
);
696 restore_flags(flags
);
700 void get_rtc_time(struct rtc_time
*rtc_tm
)
703 unsigned long flags
, uip_watchdog
= jiffies
;
707 * read RTC once any update in progress is done. The update
708 * can take just over 2ms. We wait 10 to 20ms. There is no need to
709 * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
710 * If you need to know *exactly* when a second has started, enable
711 * periodic update complete interrupts, (via ioctl) and then
712 * immediately read /dev/rtc which will block until you get the IRQ.
713 * Once the read clears, read the RTC time (again via ioctl). Easy.
716 if (rtc_is_updating() != 0)
717 while (jiffies
- uip_watchdog
< 2*HZ
/100)
721 * Only the values that we read from the RTC are set. We leave
722 * tm_wday, tm_yday and tm_isdst untouched. Even though the
723 * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
724 * by the RTC when initially set to a non-zero value.
728 rtc_tm
->tm_sec
= CMOS_READ(RTC_SECONDS
);
729 rtc_tm
->tm_min
= CMOS_READ(RTC_MINUTES
);
730 rtc_tm
->tm_hour
= CMOS_READ(RTC_HOURS
);
731 rtc_tm
->tm_mday
= CMOS_READ(RTC_DAY_OF_MONTH
);
732 rtc_tm
->tm_mon
= CMOS_READ(RTC_MONTH
);
733 rtc_tm
->tm_year
= CMOS_READ(RTC_YEAR
);
734 ctrl
= CMOS_READ(RTC_CONTROL
);
735 restore_flags(flags
);
737 if (!(ctrl
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
)
739 BCD_TO_BIN(rtc_tm
->tm_sec
);
740 BCD_TO_BIN(rtc_tm
->tm_min
);
741 BCD_TO_BIN(rtc_tm
->tm_hour
);
742 BCD_TO_BIN(rtc_tm
->tm_mday
);
743 BCD_TO_BIN(rtc_tm
->tm_mon
);
744 BCD_TO_BIN(rtc_tm
->tm_year
);
748 * Account for differences between how the RTC uses the values
749 * and how they are defined in a struct rtc_time;
751 if ((rtc_tm
->tm_year
+= (epoch
- 1900)) <= 69)
752 rtc_tm
->tm_year
+= 100;
757 void get_rtc_alm_time(struct rtc_time
*alm_tm
)
763 * Only the values that we read from the RTC are set. That
764 * means only tm_hour, tm_min, and tm_sec.
768 alm_tm
->tm_sec
= CMOS_READ(RTC_SECONDS_ALARM
);
769 alm_tm
->tm_min
= CMOS_READ(RTC_MINUTES_ALARM
);
770 alm_tm
->tm_hour
= CMOS_READ(RTC_HOURS_ALARM
);
771 ctrl
= CMOS_READ(RTC_CONTROL
);
772 restore_flags(flags
);
774 if (!(ctrl
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
)
776 BCD_TO_BIN(alm_tm
->tm_sec
);
777 BCD_TO_BIN(alm_tm
->tm_min
);
778 BCD_TO_BIN(alm_tm
->tm_hour
);
783 * Used to disable/enable interrupts for any one of UIE, AIE, PIE.
784 * Rumour has it that if you frob the interrupt enable/disable
785 * bits in RTC_CONTROL, you should read RTC_INTR_FLAGS, to
786 * ensure you actually start getting interrupts. Probably for
787 * compatibility with older/broken chipset RTC implementations.
788 * We also clear out any old irq data after an ioctl() that
789 * meddles with the interrupt enable/disable bits.
792 void mask_rtc_irq_bit(unsigned char bit
)
799 val
= CMOS_READ(RTC_CONTROL
);
801 CMOS_WRITE(val
, RTC_CONTROL
);
802 CMOS_READ(RTC_INTR_FLAGS
);
803 restore_flags(flags
);
807 void set_rtc_irq_bit(unsigned char bit
)
814 val
= CMOS_READ(RTC_CONTROL
);
816 CMOS_WRITE(val
, RTC_CONTROL
);
817 CMOS_READ(RTC_INTR_FLAGS
);
819 restore_flags(flags
);