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
44 #define RTC_VERSION "1.10c"
46 #define RTC_IRQ 8 /* Can't see this changing soon. */
47 #define RTC_IO_EXTENT 0x10 /* Only really two ports, but... */
50 * Note that *all* calls to CMOS_READ and CMOS_WRITE are done with
51 * interrupts disabled. Due to the index-port/data-port (0x70/0x71)
52 * design of the RTC, we don't want two different things trying to
53 * get to it at once. (e.g. the periodic 11 min sync from time.c vs.
57 #include <linux/module.h>
58 #include <linux/kernel.h>
59 #include <linux/types.h>
60 #include <linux/miscdevice.h>
61 #include <linux/ioport.h>
62 #include <linux/fcntl.h>
63 #include <linux/mc146818rtc.h>
64 #include <linux/init.h>
65 #include <linux/poll.h>
66 #include <linux/proc_fs.h>
67 #include <linux/spinlock.h>
70 #include <asm/uaccess.h>
71 #include <asm/system.h>
76 static unsigned long rtc_port
;
81 * We sponge a minor off of the misc major. No need slurping
82 * up another valuable major dev number for this. If you add
83 * an ioctl, make sure you don't conflict with SPARC's RTC
87 static struct fasync_struct
*rtc_async_queue
;
89 static DECLARE_WAIT_QUEUE_HEAD(rtc_wait
);
91 extern spinlock_t rtc_lock
;
93 static struct timer_list rtc_irq_timer
;
95 static loff_t
rtc_llseek(struct file
*file
, loff_t offset
, int origin
);
97 static ssize_t
rtc_read(struct file
*file
, char *buf
,
98 size_t count
, loff_t
*ppos
);
100 static int rtc_ioctl(struct inode
*inode
, struct file
*file
,
101 unsigned int cmd
, unsigned long arg
);
104 static unsigned int rtc_poll(struct file
*file
, poll_table
*wait
);
107 static void get_rtc_time (struct rtc_time
*rtc_tm
);
108 static void get_rtc_alm_time (struct rtc_time
*alm_tm
);
110 static void rtc_dropped_irq(unsigned long data
);
112 static void set_rtc_irq_bit(unsigned char bit
);
113 static void mask_rtc_irq_bit(unsigned char bit
);
116 static inline unsigned char rtc_is_updating(void);
118 static int rtc_read_proc(char *page
, char **start
, off_t off
,
119 int count
, int *eof
, void *data
);
122 * Bits in rtc_status. (6 bits of room for future expansion)
125 #define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
126 #define RTC_TIMER_ON 0x02 /* missed irq timer active */
129 * rtc_status is never changed by rtc_interrupt, and ioctl/open/close is
130 * protected by the big kernel lock. However, ioctl can still disable the timer
131 * in rtc_status and then with del_timer after the interrupt has read
132 * rtc_status but before mod_timer is called, which would then reenable the
133 * timer (but you would need to have an awful timing before you'd trip on it)
135 static unsigned long rtc_status
= 0; /* bitmapped status byte. */
136 static unsigned long rtc_freq
= 0; /* Current periodic IRQ rate */
137 static unsigned long rtc_irq_data
= 0; /* our output to the world */
140 * If this driver ever becomes modularised, it will be really nice
141 * to make the epoch retain its value across module reload...
144 static unsigned long epoch
= 1900; /* year corresponding to 0x00 */
146 static const unsigned char days_in_mo
[] =
147 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
151 * A very tiny interrupt handler. It runs with SA_INTERRUPT set,
152 * but there is possibility of conflicting with the set_rtc_mmss()
153 * call (the rtc irq and the timer irq can easily run at the same
154 * time in two different CPUs). So we need to serializes
155 * accesses to the chip with the rtc_lock spinlock that each
156 * architecture should implement in the timer code.
157 * (See ./arch/XXXX/kernel/time.c for the set_rtc_mmss() function.)
160 static void rtc_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
163 * Can be an alarm interrupt, update complete interrupt,
164 * or a periodic interrupt. We store the status in the
165 * low byte and the number of interrupts received since
166 * the last read in the remainder of rtc_irq_data.
169 spin_lock (&rtc_lock
);
170 rtc_irq_data
+= 0x100;
171 rtc_irq_data
&= ~0xff;
172 rtc_irq_data
|= (CMOS_READ(RTC_INTR_FLAGS
) & 0xF0);
174 if (rtc_status
& RTC_TIMER_ON
)
175 mod_timer(&rtc_irq_timer
, jiffies
+ HZ
/rtc_freq
+ 2*HZ
/100);
177 spin_unlock (&rtc_lock
);
179 /* Now do the rest of the actions */
180 wake_up_interruptible(&rtc_wait
);
182 kill_fasync (&rtc_async_queue
, SIGIO
, POLL_IN
);
187 * Now all the various file operations that we export.
190 static loff_t
rtc_llseek(struct file
*file
, loff_t offset
, int origin
)
195 static ssize_t
rtc_read(struct file
*file
, char *buf
,
196 size_t count
, loff_t
*ppos
)
201 DECLARE_WAITQUEUE(wait
, current
);
205 if (count
< sizeof(unsigned long))
208 add_wait_queue(&rtc_wait
, &wait
);
210 current
->state
= TASK_INTERRUPTIBLE
;
213 /* First make it right. Then make it fast. Putting this whole
214 * block within the parentheses of a while would be too
215 * confusing. And no, xchg() is not the answer. */
216 spin_lock_irq (&rtc_lock
);
219 spin_unlock_irq (&rtc_lock
);
224 if (file
->f_flags
& O_NONBLOCK
) {
228 if (signal_pending(current
)) {
229 retval
= -ERESTARTSYS
;
235 retval
= put_user(data
, (unsigned long *)buf
);
237 retval
= sizeof(unsigned long);
239 current
->state
= TASK_RUNNING
;
240 remove_wait_queue(&rtc_wait
, &wait
);
246 static int rtc_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
,
249 struct rtc_time wtime
;
253 case RTC_AIE_OFF
: /* Mask alarm int. enab. bit */
255 mask_rtc_irq_bit(RTC_AIE
);
258 case RTC_AIE_ON
: /* Allow alarm interrupts. */
260 set_rtc_irq_bit(RTC_AIE
);
263 case RTC_PIE_OFF
: /* Mask periodic int. enab. bit */
265 mask_rtc_irq_bit(RTC_PIE
);
266 if (rtc_status
& RTC_TIMER_ON
) {
267 spin_lock_irq (&rtc_lock
);
268 rtc_status
&= ~RTC_TIMER_ON
;
269 del_timer(&rtc_irq_timer
);
270 spin_unlock_irq (&rtc_lock
);
274 case RTC_PIE_ON
: /* Allow periodic ints */
278 * We don't really want Joe User enabling more
279 * than 64Hz of interrupts on a multi-user machine.
281 if ((rtc_freq
> 64) && (!capable(CAP_SYS_RESOURCE
)))
284 if (!(rtc_status
& RTC_TIMER_ON
)) {
285 spin_lock_irq (&rtc_lock
);
286 rtc_irq_timer
.expires
= jiffies
+ HZ
/rtc_freq
+ 2*HZ
/100;
287 add_timer(&rtc_irq_timer
);
288 rtc_status
|= RTC_TIMER_ON
;
289 spin_unlock_irq (&rtc_lock
);
291 set_rtc_irq_bit(RTC_PIE
);
294 case RTC_UIE_OFF
: /* Mask ints from RTC updates. */
296 mask_rtc_irq_bit(RTC_UIE
);
299 case RTC_UIE_ON
: /* Allow ints for RTC updates. */
301 set_rtc_irq_bit(RTC_UIE
);
305 case RTC_ALM_READ
: /* Read the present alarm time */
308 * This returns a struct rtc_time. Reading >= 0xc0
309 * means "don't care" or "match all". Only the tm_hour,
310 * tm_min, and tm_sec values are filled in.
313 get_rtc_alm_time(&wtime
);
316 case RTC_ALM_SET
: /* Store a time into the alarm */
319 * This expects a struct rtc_time. Writing 0xff means
320 * "don't care" or "match all". Only the tm_hour,
321 * tm_min and tm_sec are used.
323 unsigned char hrs
, min
, sec
;
324 struct rtc_time alm_tm
;
326 if (copy_from_user(&alm_tm
, (struct rtc_time
*)arg
,
327 sizeof(struct rtc_time
)))
330 hrs
= alm_tm
.tm_hour
;
343 spin_lock_irq(&rtc_lock
);
344 if (!(CMOS_READ(RTC_CONTROL
) & RTC_DM_BINARY
) ||
351 CMOS_WRITE(hrs
, RTC_HOURS_ALARM
);
352 CMOS_WRITE(min
, RTC_MINUTES_ALARM
);
353 CMOS_WRITE(sec
, RTC_SECONDS_ALARM
);
354 spin_unlock_irq(&rtc_lock
);
358 case RTC_RD_TIME
: /* Read the time/date from RTC */
360 get_rtc_time(&wtime
);
363 case RTC_SET_TIME
: /* Set the RTC */
365 struct rtc_time rtc_tm
;
366 unsigned char mon
, day
, hrs
, min
, sec
, leap_yr
;
367 unsigned char save_control
, save_freq_select
;
370 if (!capable(CAP_SYS_TIME
))
373 if (copy_from_user(&rtc_tm
, (struct rtc_time
*)arg
,
374 sizeof(struct rtc_time
)))
377 yrs
= rtc_tm
.tm_year
+ 1900;
378 mon
= rtc_tm
.tm_mon
+ 1; /* tm_mon starts at zero */
379 day
= rtc_tm
.tm_mday
;
380 hrs
= rtc_tm
.tm_hour
;
387 leap_yr
= ((!(yrs
% 4) && (yrs
% 100)) || !(yrs
% 400));
389 if ((mon
> 12) || (day
== 0))
392 if (day
> (days_in_mo
[mon
] + ((mon
== 2) && leap_yr
)))
395 if ((hrs
>= 24) || (min
>= 60) || (sec
>= 60))
398 if ((yrs
-= epoch
) > 255) /* They are unsigned */
401 spin_lock_irq(&rtc_lock
);
402 if (!(CMOS_READ(RTC_CONTROL
) & RTC_DM_BINARY
)
405 spin_unlock_irq(&rtc_lock
);
419 save_control
= CMOS_READ(RTC_CONTROL
);
420 CMOS_WRITE((save_control
|RTC_SET
), RTC_CONTROL
);
421 save_freq_select
= CMOS_READ(RTC_FREQ_SELECT
);
422 CMOS_WRITE((save_freq_select
|RTC_DIV_RESET2
), RTC_FREQ_SELECT
);
424 CMOS_WRITE(yrs
, RTC_YEAR
);
425 CMOS_WRITE(mon
, RTC_MONTH
);
426 CMOS_WRITE(day
, RTC_DAY_OF_MONTH
);
427 CMOS_WRITE(hrs
, RTC_HOURS
);
428 CMOS_WRITE(min
, RTC_MINUTES
);
429 CMOS_WRITE(sec
, RTC_SECONDS
);
431 CMOS_WRITE(save_control
, RTC_CONTROL
);
432 CMOS_WRITE(save_freq_select
, RTC_FREQ_SELECT
);
434 spin_unlock_irq(&rtc_lock
);
438 case RTC_IRQP_READ
: /* Read the periodic IRQ rate. */
440 return put_user(rtc_freq
, (unsigned long *)arg
);
442 case RTC_IRQP_SET
: /* Set periodic IRQ rate. */
448 * The max we can do is 8192Hz.
450 if ((arg
< 2) || (arg
> 8192))
453 * We don't really want Joe User generating more
454 * than 64Hz of interrupts on a multi-user machine.
456 if ((arg
> 64) && (!capable(CAP_SYS_RESOURCE
)))
459 while (arg
> (1<<tmp
))
463 * Check that the input was really a power of 2.
468 spin_lock_irq(&rtc_lock
);
471 val
= CMOS_READ(RTC_FREQ_SELECT
) & 0xf0;
473 CMOS_WRITE(val
, RTC_FREQ_SELECT
);
474 spin_unlock_irq(&rtc_lock
);
478 case RTC_EPOCH_READ
: /* Read the epoch. */
480 return put_user (epoch
, (unsigned long *)arg
);
482 case RTC_EPOCH_SET
: /* Set the epoch. */
485 * There were no RTC clocks before 1900.
490 if (!capable(CAP_SYS_TIME
))
500 return copy_to_user((void *)arg
, &wtime
, sizeof wtime
) ? -EFAULT
: 0;
504 * We enforce only one user at a time here with the open/close.
505 * Also clear the previous interrupt data on an open, and clean
506 * up things on a close.
509 static int rtc_open(struct inode
*inode
, struct file
*file
)
511 /* If someday somebody decides to remove the kernel_lock on open and
512 * close and ioctl this is gonna get open to races */
513 if(rtc_status
& RTC_IS_OPEN
)
516 rtc_status
|= RTC_IS_OPEN
;
518 spin_lock_irq (&rtc_lock
);
520 spin_unlock_irq (&rtc_lock
);
524 static int rtc_fasync (int fd
, struct file
*filp
, int on
)
527 return fasync_helper (fd
, filp
, on
, &rtc_async_queue
);
530 static int rtc_release(struct inode
*inode
, struct file
*file
)
534 * Turn off all interrupts once the device is no longer
535 * in use, and clear the data.
540 spin_lock_irq(&rtc_lock
);
541 tmp
= CMOS_READ(RTC_CONTROL
);
545 CMOS_WRITE(tmp
, RTC_CONTROL
);
546 CMOS_READ(RTC_INTR_FLAGS
);
548 if (rtc_status
& RTC_TIMER_ON
) {
549 rtc_status
&= ~RTC_TIMER_ON
;
550 del_timer(&rtc_irq_timer
);
552 spin_unlock_irq(&rtc_lock
);
554 if (file
->f_flags
& FASYNC
) {
555 rtc_fasync (-1, file
, 0);
560 spin_lock_irq (&rtc_lock
);
562 spin_unlock_irq (&rtc_lock
);
563 rtc_status
= rtc_status
& ~RTC_IS_OPEN
;
568 /* Called without the kernel lock - fine */
569 static unsigned int rtc_poll(struct file
*file
, poll_table
*wait
)
573 poll_wait(file
, &rtc_wait
, wait
);
575 spin_lock_irq (&rtc_lock
);
577 spin_unlock_irq (&rtc_lock
);
580 return POLLIN
| POLLRDNORM
;
586 * The various file operations we support.
589 static struct file_operations rtc_fops
= {
598 release
: rtc_release
,
602 static struct miscdevice rtc_dev
=
609 static int __init
rtc_init(void)
612 unsigned int year
, ctrl
;
613 unsigned long uip_watchdog
;
617 struct linux_ebus
*ebus
;
618 struct linux_ebus_device
*edev
;
622 for_each_ebus(ebus
) {
623 for_each_ebusdev(edev
, ebus
) {
624 if(strcmp(edev
->prom_name
, "rtc") == 0) {
629 printk("rtc_init: no PC rtc found\n");
633 rtc_port
= edev
->resource
[0].start
;
634 rtc_irq
= edev
->irqs
[0];
636 * XXX Interrupt pin #7 in Espresso is shared between RTC and
637 * PCI Slot 2 INTA# (and some INTx# in Slot 1). SA_INTERRUPT here
638 * is asking for trouble with add-on boards. Change to SA_SHIRQ.
640 if(request_irq(rtc_irq
, rtc_interrupt
, SA_INTERRUPT
, "rtc", (void *)&rtc_port
)) {
642 * Standard way for sparc to print irq's is to use
643 * __irq_itoa(). I think for EBus it's ok to use %d.
645 printk("rtc: cannot register IRQ %d\n", rtc_irq
);
649 if (check_region (RTC_PORT (0), RTC_IO_EXTENT
))
651 printk(KERN_ERR
"rtc: I/O port %d is not free.\n", RTC_PORT (0));
656 if(request_irq(RTC_IRQ
, rtc_interrupt
, SA_INTERRUPT
, "rtc", NULL
))
658 /* Yeah right, seeing as irq 8 doesn't even hit the bus. */
659 printk(KERN_ERR
"rtc: IRQ %d is not free.\n", RTC_IRQ
);
664 request_region(RTC_PORT(0), RTC_IO_EXTENT
, "rtc");
665 #endif /* __sparc__ vs. others */
667 misc_register(&rtc_dev
);
668 create_proc_read_entry ("driver/rtc", 0, 0, rtc_read_proc
, NULL
);
673 /* Each operating system on an Alpha uses its own epoch.
674 Let's try to guess which one we are using now. */
676 uip_watchdog
= jiffies
;
677 if (rtc_is_updating() != 0)
678 while (jiffies
- uip_watchdog
< 2*HZ
/100)
681 spin_lock_irq(&rtc_lock
);
682 year
= CMOS_READ(RTC_YEAR
);
683 ctrl
= CMOS_READ(RTC_CONTROL
);
684 spin_unlock_irq(&rtc_lock
);
686 if (!(ctrl
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
)
687 BCD_TO_BIN(year
); /* This should never happen... */
689 if (year
>= 20 && year
< 48) {
691 guess
= "ARC console";
692 } else if (year
>= 48 && year
< 100) {
694 guess
= "Digital UNIX";
697 printk("rtc: %s epoch (%lu) detected\n", guess
, epoch
);
700 init_timer(&rtc_irq_timer
);
701 rtc_irq_timer
.function
= rtc_dropped_irq
;
702 spin_lock_irq(&rtc_lock
);
703 /* Initialize periodic freq. to CMOS reset default, which is 1024Hz */
704 CMOS_WRITE(((CMOS_READ(RTC_FREQ_SELECT
) & 0xF0) | 0x06), RTC_FREQ_SELECT
);
705 spin_unlock_irq(&rtc_lock
);
709 printk(KERN_INFO
"Real Time Clock Driver v" RTC_VERSION
"\n");
714 static void __exit
rtc_exit (void)
716 /* interrupts and maybe timer disabled at this point by rtc_release */
717 /* FIXME: Maybe??? */
719 if (rtc_status
& RTC_TIMER_ON
) {
720 spin_lock_irq (&rtc_lock
);
721 rtc_status
&= ~RTC_TIMER_ON
;
722 del_timer(&rtc_irq_timer
);
723 spin_unlock_irq (&rtc_lock
);
725 printk(KERN_WARNING
"rtc_exit(), and timer still running.\n");
728 remove_proc_entry ("driver/rtc", NULL
);
729 misc_deregister(&rtc_dev
);
732 free_irq (rtc_irq
, &rtc_port
);
734 release_region (RTC_PORT (0), RTC_IO_EXTENT
);
736 free_irq (RTC_IRQ
, NULL
);
738 #endif /* __sparc__ */
741 module_init(rtc_init
);
742 module_exit(rtc_exit
);
747 * At IRQ rates >= 4096Hz, an interrupt may get lost altogether.
748 * (usually during an IDE disk interrupt, with IRQ unmasking off)
749 * Since the interrupt handler doesn't get called, the IRQ status
750 * byte doesn't get read, and the RTC stops generating interrupts.
751 * A timer is set, and will call this function if/when that happens.
752 * To get it out of this stalled state, we just read the status.
753 * At least a jiffy of interrupts (rtc_freq/HZ) will have been lost.
754 * (You *really* shouldn't be trying to use a non-realtime system
755 * for something that requires a steady > 1KHz signal anyways.)
758 static void rtc_dropped_irq(unsigned long data
)
762 spin_lock_irq (&rtc_lock
);
764 /* Just in case someone disabled the timer from behind our back... */
765 if (rtc_status
& RTC_TIMER_ON
)
766 mod_timer(&rtc_irq_timer
, jiffies
+ HZ
/rtc_freq
+ 2*HZ
/100);
768 rtc_irq_data
+= ((rtc_freq
/HZ
)<<8);
769 rtc_irq_data
&= ~0xff;
770 rtc_irq_data
|= (CMOS_READ(RTC_INTR_FLAGS
) & 0xF0); /* restart */
774 spin_unlock_irq(&rtc_lock
);
776 printk(KERN_INFO
"rtc: lost some interrupts at %ldHz.\n", freq
);
778 /* Now we have new data */
779 wake_up_interruptible(&rtc_wait
);
781 kill_fasync (&rtc_async_queue
, SIGIO
, POLL_IN
);
786 * Info exported via "/proc/driver/rtc".
789 static int rtc_proc_output (char *buf
)
791 #define YN(bit) ((ctrl & bit) ? "yes" : "no")
792 #define NY(bit) ((ctrl & bit) ? "no" : "yes")
795 unsigned char batt
, ctrl
;
798 spin_lock_irq(&rtc_lock
);
799 batt
= CMOS_READ(RTC_VALID
) & RTC_VRT
;
800 ctrl
= CMOS_READ(RTC_CONTROL
);
802 spin_unlock_irq(&rtc_lock
);
809 * There is no way to tell if the luser has the RTC set for local
810 * time or for Universal Standard Time (GMT). Probably local though.
813 "rtc_time\t: %02d:%02d:%02d\n"
814 "rtc_date\t: %04d-%02d-%02d\n"
815 "rtc_epoch\t: %04lu\n",
816 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
,
817 tm
.tm_year
+ 1900, tm
.tm_mon
+ 1, tm
.tm_mday
, epoch
);
819 get_rtc_alm_time(&tm
);
822 * We implicitly assume 24hr mode here. Alarm values >= 0xc0 will
823 * match any value for that particular field. Values that are
824 * greater than a valid time, but less than 0xc0 shouldn't appear.
826 p
+= sprintf(p
, "alarm\t\t: ");
827 if (tm
.tm_hour
<= 24)
828 p
+= sprintf(p
, "%02d:", tm
.tm_hour
);
830 p
+= sprintf(p
, "**:");
833 p
+= sprintf(p
, "%02d:", tm
.tm_min
);
835 p
+= sprintf(p
, "**:");
838 p
+= sprintf(p
, "%02d\n", tm
.tm_sec
);
840 p
+= sprintf(p
, "**\n");
846 "square_wave\t: %s\n"
849 "periodic_IRQ\t: %s\n"
850 "periodic_freq\t: %ld\n"
851 "batt_status\t: %s\n",
860 batt
? "okay" : "dead");
867 static int rtc_read_proc(char *page
, char **start
, off_t off
,
868 int count
, int *eof
, void *data
)
870 int len
= rtc_proc_output (page
);
871 if (len
<= off
+count
) *eof
= 1;
874 if (len
>count
) len
= count
;
880 * Returns true if a clock update is in progress
882 /* FIXME shouldn't this be above rtc_init to make it fully inlined? */
883 static inline unsigned char rtc_is_updating(void)
887 spin_lock_irq(&rtc_lock
);
888 uip
= (CMOS_READ(RTC_FREQ_SELECT
) & RTC_UIP
);
889 spin_unlock_irq(&rtc_lock
);
893 static void get_rtc_time(struct rtc_time
*rtc_tm
)
895 unsigned long uip_watchdog
= jiffies
;
899 * read RTC once any update in progress is done. The update
900 * can take just over 2ms. We wait 10 to 20ms. There is no need to
901 * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
902 * If you need to know *exactly* when a second has started, enable
903 * periodic update complete interrupts, (via ioctl) and then
904 * immediately read /dev/rtc which will block until you get the IRQ.
905 * Once the read clears, read the RTC time (again via ioctl). Easy.
908 if (rtc_is_updating() != 0)
909 while (jiffies
- uip_watchdog
< 2*HZ
/100)
913 * Only the values that we read from the RTC are set. We leave
914 * tm_wday, tm_yday and tm_isdst untouched. Even though the
915 * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
916 * by the RTC when initially set to a non-zero value.
918 spin_lock_irq(&rtc_lock
);
919 rtc_tm
->tm_sec
= CMOS_READ(RTC_SECONDS
);
920 rtc_tm
->tm_min
= CMOS_READ(RTC_MINUTES
);
921 rtc_tm
->tm_hour
= CMOS_READ(RTC_HOURS
);
922 rtc_tm
->tm_mday
= CMOS_READ(RTC_DAY_OF_MONTH
);
923 rtc_tm
->tm_mon
= CMOS_READ(RTC_MONTH
);
924 rtc_tm
->tm_year
= CMOS_READ(RTC_YEAR
);
925 ctrl
= CMOS_READ(RTC_CONTROL
);
926 spin_unlock_irq(&rtc_lock
);
928 if (!(ctrl
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
)
930 BCD_TO_BIN(rtc_tm
->tm_sec
);
931 BCD_TO_BIN(rtc_tm
->tm_min
);
932 BCD_TO_BIN(rtc_tm
->tm_hour
);
933 BCD_TO_BIN(rtc_tm
->tm_mday
);
934 BCD_TO_BIN(rtc_tm
->tm_mon
);
935 BCD_TO_BIN(rtc_tm
->tm_year
);
939 * Account for differences between how the RTC uses the values
940 * and how they are defined in a struct rtc_time;
942 if ((rtc_tm
->tm_year
+= (epoch
- 1900)) <= 69)
943 rtc_tm
->tm_year
+= 100;
948 static void get_rtc_alm_time(struct rtc_time
*alm_tm
)
953 * Only the values that we read from the RTC are set. That
954 * means only tm_hour, tm_min, and tm_sec.
956 spin_lock_irq(&rtc_lock
);
957 alm_tm
->tm_sec
= CMOS_READ(RTC_SECONDS_ALARM
);
958 alm_tm
->tm_min
= CMOS_READ(RTC_MINUTES_ALARM
);
959 alm_tm
->tm_hour
= CMOS_READ(RTC_HOURS_ALARM
);
960 ctrl
= CMOS_READ(RTC_CONTROL
);
961 spin_unlock_irq(&rtc_lock
);
963 if (!(ctrl
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
)
965 BCD_TO_BIN(alm_tm
->tm_sec
);
966 BCD_TO_BIN(alm_tm
->tm_min
);
967 BCD_TO_BIN(alm_tm
->tm_hour
);
973 * Used to disable/enable interrupts for any one of UIE, AIE, PIE.
974 * Rumour has it that if you frob the interrupt enable/disable
975 * bits in RTC_CONTROL, you should read RTC_INTR_FLAGS, to
976 * ensure you actually start getting interrupts. Probably for
977 * compatibility with older/broken chipset RTC implementations.
978 * We also clear out any old irq data after an ioctl() that
979 * meddles with the interrupt enable/disable bits.
982 static void mask_rtc_irq_bit(unsigned char bit
)
986 spin_lock_irq(&rtc_lock
);
987 val
= CMOS_READ(RTC_CONTROL
);
989 CMOS_WRITE(val
, RTC_CONTROL
);
990 CMOS_READ(RTC_INTR_FLAGS
);
993 spin_unlock_irq(&rtc_lock
);
996 static void set_rtc_irq_bit(unsigned char bit
)
1000 spin_lock_irq(&rtc_lock
);
1001 val
= CMOS_READ(RTC_CONTROL
);
1003 CMOS_WRITE(val
, RTC_CONTROL
);
1004 CMOS_READ(RTC_INTR_FLAGS
);
1007 spin_unlock_irq(&rtc_lock
);