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
45 #define RTC_VERSION "1.10d"
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/config.h>
58 #include <linux/module.h>
59 #include <linux/kernel.h>
60 #include <linux/types.h>
61 #include <linux/miscdevice.h>
62 #include <linux/ioport.h>
63 #include <linux/fcntl.h>
64 #include <linux/mc146818rtc.h>
65 #include <linux/init.h>
66 #include <linux/poll.h>
67 #include <linux/proc_fs.h>
68 #include <linux/spinlock.h>
71 #include <asm/uaccess.h>
72 #include <asm/system.h>
77 static unsigned long rtc_port
;
82 * We sponge a minor off of the misc major. No need slurping
83 * up another valuable major dev number for this. If you add
84 * an ioctl, make sure you don't conflict with SPARC's RTC
88 static struct fasync_struct
*rtc_async_queue
;
90 static DECLARE_WAIT_QUEUE_HEAD(rtc_wait
);
92 extern spinlock_t rtc_lock
;
94 static struct timer_list rtc_irq_timer
;
96 static loff_t
rtc_llseek(struct file
*file
, loff_t offset
, int origin
);
98 static ssize_t
rtc_read(struct file
*file
, char *buf
,
99 size_t count
, loff_t
*ppos
);
101 static int rtc_ioctl(struct inode
*inode
, struct file
*file
,
102 unsigned int cmd
, unsigned long arg
);
105 static unsigned int rtc_poll(struct file
*file
, poll_table
*wait
);
108 static void get_rtc_time (struct rtc_time
*rtc_tm
);
109 static void get_rtc_alm_time (struct rtc_time
*alm_tm
);
111 static void rtc_dropped_irq(unsigned long data
);
113 static void set_rtc_irq_bit(unsigned char bit
);
114 static void mask_rtc_irq_bit(unsigned char bit
);
117 static inline unsigned char rtc_is_updating(void);
119 static int rtc_read_proc(char *page
, char **start
, off_t off
,
120 int count
, int *eof
, void *data
);
123 * Bits in rtc_status. (6 bits of room for future expansion)
126 #define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
127 #define RTC_TIMER_ON 0x02 /* missed irq timer active */
130 * rtc_status is never changed by rtc_interrupt, and ioctl/open/close is
131 * protected by the big kernel lock. However, ioctl can still disable the timer
132 * in rtc_status and then with del_timer after the interrupt has read
133 * rtc_status but before mod_timer is called, which would then reenable the
134 * timer (but you would need to have an awful timing before you'd trip on it)
136 static unsigned long rtc_status
= 0; /* bitmapped status byte. */
137 static unsigned long rtc_freq
= 0; /* Current periodic IRQ rate */
138 static unsigned long rtc_irq_data
= 0; /* our output to the world */
141 * If this driver ever becomes modularised, it will be really nice
142 * to make the epoch retain its value across module reload...
145 static unsigned long epoch
= 1900; /* year corresponding to 0x00 */
147 static const unsigned char days_in_mo
[] =
148 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
152 * A very tiny interrupt handler. It runs with SA_INTERRUPT set,
153 * but there is possibility of conflicting with the set_rtc_mmss()
154 * call (the rtc irq and the timer irq can easily run at the same
155 * time in two different CPUs). So we need to serializes
156 * accesses to the chip with the rtc_lock spinlock that each
157 * architecture should implement in the timer code.
158 * (See ./arch/XXXX/kernel/time.c for the set_rtc_mmss() function.)
161 static void rtc_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
164 * Can be an alarm interrupt, update complete interrupt,
165 * or a periodic interrupt. We store the status in the
166 * low byte and the number of interrupts received since
167 * the last read in the remainder of rtc_irq_data.
170 spin_lock (&rtc_lock
);
171 rtc_irq_data
+= 0x100;
172 rtc_irq_data
&= ~0xff;
173 rtc_irq_data
|= (CMOS_READ(RTC_INTR_FLAGS
) & 0xF0);
175 if (rtc_status
& RTC_TIMER_ON
)
176 mod_timer(&rtc_irq_timer
, jiffies
+ HZ
/rtc_freq
+ 2*HZ
/100);
178 spin_unlock (&rtc_lock
);
180 /* Now do the rest of the actions */
181 wake_up_interruptible(&rtc_wait
);
183 kill_fasync (&rtc_async_queue
, SIGIO
, POLL_IN
);
188 * Now all the various file operations that we export.
191 static loff_t
rtc_llseek(struct file
*file
, loff_t offset
, int origin
)
196 static ssize_t
rtc_read(struct file
*file
, char *buf
,
197 size_t count
, loff_t
*ppos
)
202 DECLARE_WAITQUEUE(wait
, current
);
206 if (count
< sizeof(unsigned long))
209 add_wait_queue(&rtc_wait
, &wait
);
211 current
->state
= TASK_INTERRUPTIBLE
;
214 /* First make it right. Then make it fast. Putting this whole
215 * block within the parentheses of a while would be too
216 * confusing. And no, xchg() is not the answer. */
217 spin_lock_irq (&rtc_lock
);
220 spin_unlock_irq (&rtc_lock
);
225 if (file
->f_flags
& O_NONBLOCK
) {
229 if (signal_pending(current
)) {
230 retval
= -ERESTARTSYS
;
236 retval
= put_user(data
, (unsigned long *)buf
);
238 retval
= sizeof(unsigned long);
240 current
->state
= TASK_RUNNING
;
241 remove_wait_queue(&rtc_wait
, &wait
);
247 static int rtc_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
,
250 struct rtc_time wtime
;
254 case RTC_AIE_OFF
: /* Mask alarm int. enab. bit */
256 mask_rtc_irq_bit(RTC_AIE
);
259 case RTC_AIE_ON
: /* Allow alarm interrupts. */
261 set_rtc_irq_bit(RTC_AIE
);
264 case RTC_PIE_OFF
: /* Mask periodic int. enab. bit */
266 mask_rtc_irq_bit(RTC_PIE
);
267 if (rtc_status
& RTC_TIMER_ON
) {
268 spin_lock_irq (&rtc_lock
);
269 rtc_status
&= ~RTC_TIMER_ON
;
270 del_timer(&rtc_irq_timer
);
271 spin_unlock_irq (&rtc_lock
);
275 case RTC_PIE_ON
: /* Allow periodic ints */
279 * We don't really want Joe User enabling more
280 * than 64Hz of interrupts on a multi-user machine.
282 if ((rtc_freq
> 64) && (!capable(CAP_SYS_RESOURCE
)))
285 if (!(rtc_status
& RTC_TIMER_ON
)) {
286 spin_lock_irq (&rtc_lock
);
287 rtc_irq_timer
.expires
= jiffies
+ HZ
/rtc_freq
+ 2*HZ
/100;
288 add_timer(&rtc_irq_timer
);
289 rtc_status
|= RTC_TIMER_ON
;
290 spin_unlock_irq (&rtc_lock
);
292 set_rtc_irq_bit(RTC_PIE
);
295 case RTC_UIE_OFF
: /* Mask ints from RTC updates. */
297 mask_rtc_irq_bit(RTC_UIE
);
300 case RTC_UIE_ON
: /* Allow ints for RTC updates. */
302 set_rtc_irq_bit(RTC_UIE
);
306 case RTC_ALM_READ
: /* Read the present alarm time */
309 * This returns a struct rtc_time. Reading >= 0xc0
310 * means "don't care" or "match all". Only the tm_hour,
311 * tm_min, and tm_sec values are filled in.
314 get_rtc_alm_time(&wtime
);
317 case RTC_ALM_SET
: /* Store a time into the alarm */
320 * This expects a struct rtc_time. Writing 0xff means
321 * "don't care" or "match all". Only the tm_hour,
322 * tm_min and tm_sec are used.
324 unsigned char hrs
, min
, sec
;
325 struct rtc_time alm_tm
;
327 if (copy_from_user(&alm_tm
, (struct rtc_time
*)arg
,
328 sizeof(struct rtc_time
)))
331 hrs
= alm_tm
.tm_hour
;
344 spin_lock_irq(&rtc_lock
);
345 if (!(CMOS_READ(RTC_CONTROL
) & RTC_DM_BINARY
) ||
352 CMOS_WRITE(hrs
, RTC_HOURS_ALARM
);
353 CMOS_WRITE(min
, RTC_MINUTES_ALARM
);
354 CMOS_WRITE(sec
, RTC_SECONDS_ALARM
);
355 spin_unlock_irq(&rtc_lock
);
359 case RTC_RD_TIME
: /* Read the time/date from RTC */
361 get_rtc_time(&wtime
);
364 case RTC_SET_TIME
: /* Set the RTC */
366 struct rtc_time rtc_tm
;
367 unsigned char mon
, day
, hrs
, min
, sec
, leap_yr
;
368 unsigned char save_control
, save_freq_select
;
371 if (!capable(CAP_SYS_TIME
))
374 if (copy_from_user(&rtc_tm
, (struct rtc_time
*)arg
,
375 sizeof(struct rtc_time
)))
378 yrs
= rtc_tm
.tm_year
+ 1900;
379 mon
= rtc_tm
.tm_mon
+ 1; /* tm_mon starts at zero */
380 day
= rtc_tm
.tm_mday
;
381 hrs
= rtc_tm
.tm_hour
;
388 leap_yr
= ((!(yrs
% 4) && (yrs
% 100)) || !(yrs
% 400));
390 if ((mon
> 12) || (day
== 0))
393 if (day
> (days_in_mo
[mon
] + ((mon
== 2) && leap_yr
)))
396 if ((hrs
>= 24) || (min
>= 60) || (sec
>= 60))
399 if ((yrs
-= epoch
) > 255) /* They are unsigned */
402 spin_lock_irq(&rtc_lock
);
403 if (!(CMOS_READ(RTC_CONTROL
) & RTC_DM_BINARY
)
406 spin_unlock_irq(&rtc_lock
);
420 save_control
= CMOS_READ(RTC_CONTROL
);
421 CMOS_WRITE((save_control
|RTC_SET
), RTC_CONTROL
);
422 save_freq_select
= CMOS_READ(RTC_FREQ_SELECT
);
423 CMOS_WRITE((save_freq_select
|RTC_DIV_RESET2
), RTC_FREQ_SELECT
);
425 CMOS_WRITE(yrs
, RTC_YEAR
);
426 CMOS_WRITE(mon
, RTC_MONTH
);
427 CMOS_WRITE(day
, RTC_DAY_OF_MONTH
);
428 CMOS_WRITE(hrs
, RTC_HOURS
);
429 CMOS_WRITE(min
, RTC_MINUTES
);
430 CMOS_WRITE(sec
, RTC_SECONDS
);
432 CMOS_WRITE(save_control
, RTC_CONTROL
);
433 CMOS_WRITE(save_freq_select
, RTC_FREQ_SELECT
);
435 spin_unlock_irq(&rtc_lock
);
439 case RTC_IRQP_READ
: /* Read the periodic IRQ rate. */
441 return put_user(rtc_freq
, (unsigned long *)arg
);
443 case RTC_IRQP_SET
: /* Set periodic IRQ rate. */
449 * The max we can do is 8192Hz.
451 if ((arg
< 2) || (arg
> 8192))
454 * We don't really want Joe User generating more
455 * than 64Hz of interrupts on a multi-user machine.
457 if ((arg
> 64) && (!capable(CAP_SYS_RESOURCE
)))
460 while (arg
> (1<<tmp
))
464 * Check that the input was really a power of 2.
469 spin_lock_irq(&rtc_lock
);
472 val
= CMOS_READ(RTC_FREQ_SELECT
) & 0xf0;
474 CMOS_WRITE(val
, RTC_FREQ_SELECT
);
475 spin_unlock_irq(&rtc_lock
);
478 #elif !defined(CONFIG_DECSTATION)
479 case RTC_EPOCH_READ
: /* Read the epoch. */
481 return put_user (epoch
, (unsigned long *)arg
);
483 case RTC_EPOCH_SET
: /* Set the epoch. */
486 * There were no RTC clocks before 1900.
491 if (!capable(CAP_SYS_TIME
))
501 return copy_to_user((void *)arg
, &wtime
, sizeof wtime
) ? -EFAULT
: 0;
505 * We enforce only one user at a time here with the open/close.
506 * Also clear the previous interrupt data on an open, and clean
507 * up things on a close.
510 /* We use rtc_lock to protect against concurrent opens. So the BKL is not
511 * needed here. Or anywhere else in this driver. */
512 static int rtc_open(struct inode
*inode
, struct file
*file
)
514 spin_lock_irq (&rtc_lock
);
516 if(rtc_status
& RTC_IS_OPEN
)
519 rtc_status
|= RTC_IS_OPEN
;
522 spin_unlock_irq (&rtc_lock
);
526 spin_unlock_irq (&rtc_lock
);
530 static int rtc_fasync (int fd
, struct file
*filp
, int on
)
533 return fasync_helper (fd
, filp
, on
, &rtc_async_queue
);
536 static int rtc_release(struct inode
*inode
, struct file
*file
)
540 * Turn off all interrupts once the device is no longer
541 * in use, and clear the data.
546 spin_lock_irq(&rtc_lock
);
547 tmp
= CMOS_READ(RTC_CONTROL
);
551 CMOS_WRITE(tmp
, RTC_CONTROL
);
552 CMOS_READ(RTC_INTR_FLAGS
);
554 if (rtc_status
& RTC_TIMER_ON
) {
555 rtc_status
&= ~RTC_TIMER_ON
;
556 del_timer(&rtc_irq_timer
);
558 spin_unlock_irq(&rtc_lock
);
560 if (file
->f_flags
& FASYNC
) {
561 rtc_fasync (-1, file
, 0);
565 spin_lock_irq (&rtc_lock
);
567 spin_unlock_irq (&rtc_lock
);
569 /* No need for locking -- nobody else can do anything until this rmw is
570 * committed, and no timer is running. */
571 rtc_status
&= ~RTC_IS_OPEN
;
576 /* Called without the kernel lock - fine */
577 static unsigned int rtc_poll(struct file
*file
, poll_table
*wait
)
581 poll_wait(file
, &rtc_wait
, wait
);
583 spin_lock_irq (&rtc_lock
);
585 spin_unlock_irq (&rtc_lock
);
588 return POLLIN
| POLLRDNORM
;
594 * The various file operations we support.
597 static struct file_operations rtc_fops
= {
606 release
: rtc_release
,
610 static struct miscdevice rtc_dev
=
617 static int __init
rtc_init(void)
619 #if defined(__alpha__) || defined(__mips__)
620 unsigned int year
, ctrl
;
621 unsigned long uip_watchdog
;
625 struct linux_ebus
*ebus
;
626 struct linux_ebus_device
*edev
;
630 for_each_ebus(ebus
) {
631 for_each_ebusdev(edev
, ebus
) {
632 if(strcmp(edev
->prom_name
, "rtc") == 0) {
637 printk(KERN_ERR
"rtc_init: no PC rtc found\n");
641 rtc_port
= edev
->resource
[0].start
;
642 rtc_irq
= edev
->irqs
[0];
644 * XXX Interrupt pin #7 in Espresso is shared between RTC and
645 * PCI Slot 2 INTA# (and some INTx# in Slot 1). SA_INTERRUPT here
646 * is asking for trouble with add-on boards. Change to SA_SHIRQ.
648 if(request_irq(rtc_irq
, rtc_interrupt
, SA_INTERRUPT
, "rtc", (void *)&rtc_port
)) {
650 * Standard way for sparc to print irq's is to use
651 * __irq_itoa(). I think for EBus it's ok to use %d.
653 printk(KERN_ERR
"rtc: cannot register IRQ %d\n", rtc_irq
);
657 if (check_region (RTC_PORT (0), RTC_IO_EXTENT
))
659 printk(KERN_ERR
"rtc: I/O port %d is not free.\n", RTC_PORT (0));
664 if(request_irq(RTC_IRQ
, rtc_interrupt
, SA_INTERRUPT
, "rtc", NULL
))
666 /* Yeah right, seeing as irq 8 doesn't even hit the bus. */
667 printk(KERN_ERR
"rtc: IRQ %d is not free.\n", RTC_IRQ
);
672 request_region(RTC_PORT(0), RTC_IO_EXTENT
, "rtc");
673 #endif /* __sparc__ vs. others */
675 misc_register(&rtc_dev
);
676 create_proc_read_entry ("driver/rtc", 0, 0, rtc_read_proc
, NULL
);
678 #if defined(__alpha__) || defined(__mips__)
681 /* Each operating system on an Alpha uses its own epoch.
682 Let's try to guess which one we are using now. */
684 uip_watchdog
= jiffies
;
685 if (rtc_is_updating() != 0)
686 while (jiffies
- uip_watchdog
< 2*HZ
/100)
689 spin_lock_irq(&rtc_lock
);
690 year
= CMOS_READ(RTC_YEAR
);
691 ctrl
= CMOS_READ(RTC_CONTROL
);
692 spin_unlock_irq(&rtc_lock
);
694 if (!(ctrl
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
)
695 BCD_TO_BIN(year
); /* This should never happen... */
697 if (year
>= 20 && year
< 48) {
699 guess
= "ARC console";
700 } else if (year
>= 48 && year
< 70) {
702 guess
= "Digital UNIX";
703 } else if (year
>= 70 && year
< 100) {
705 guess
= "Digital DECstation";
708 printk(KERN_INFO
"rtc: %s epoch (%lu) detected\n", guess
, epoch
);
711 init_timer(&rtc_irq_timer
);
712 rtc_irq_timer
.function
= rtc_dropped_irq
;
713 spin_lock_irq(&rtc_lock
);
714 /* Initialize periodic freq. to CMOS reset default, which is 1024Hz */
715 CMOS_WRITE(((CMOS_READ(RTC_FREQ_SELECT
) & 0xF0) | 0x06), RTC_FREQ_SELECT
);
716 spin_unlock_irq(&rtc_lock
);
720 printk(KERN_INFO
"Real Time Clock Driver v" RTC_VERSION
"\n");
725 static void __exit
rtc_exit (void)
727 remove_proc_entry ("driver/rtc", NULL
);
728 misc_deregister(&rtc_dev
);
731 free_irq (rtc_irq
, &rtc_port
);
733 release_region (RTC_PORT (0), RTC_IO_EXTENT
);
735 free_irq (RTC_IRQ
, NULL
);
737 #endif /* __sparc__ */
740 module_init(rtc_init
);
741 module_exit(rtc_exit
);
746 * At IRQ rates >= 4096Hz, an interrupt may get lost altogether.
747 * (usually during an IDE disk interrupt, with IRQ unmasking off)
748 * Since the interrupt handler doesn't get called, the IRQ status
749 * byte doesn't get read, and the RTC stops generating interrupts.
750 * A timer is set, and will call this function if/when that happens.
751 * To get it out of this stalled state, we just read the status.
752 * At least a jiffy of interrupts (rtc_freq/HZ) will have been lost.
753 * (You *really* shouldn't be trying to use a non-realtime system
754 * for something that requires a steady > 1KHz signal anyways.)
757 static void rtc_dropped_irq(unsigned long data
)
761 spin_lock_irq (&rtc_lock
);
763 /* Just in case someone disabled the timer from behind our back... */
764 if (rtc_status
& RTC_TIMER_ON
)
765 mod_timer(&rtc_irq_timer
, jiffies
+ HZ
/rtc_freq
+ 2*HZ
/100);
767 rtc_irq_data
+= ((rtc_freq
/HZ
)<<8);
768 rtc_irq_data
&= ~0xff;
769 rtc_irq_data
|= (CMOS_READ(RTC_INTR_FLAGS
) & 0xF0); /* restart */
773 spin_unlock_irq(&rtc_lock
);
775 printk(KERN_WARNING
"rtc: lost some interrupts at %ldHz.\n", freq
);
777 /* Now we have new data */
778 wake_up_interruptible(&rtc_wait
);
780 kill_fasync (&rtc_async_queue
, SIGIO
, POLL_IN
);
785 * Info exported via "/proc/driver/rtc".
788 static int rtc_proc_output (char *buf
)
790 #define YN(bit) ((ctrl & bit) ? "yes" : "no")
791 #define NY(bit) ((ctrl & bit) ? "no" : "yes")
794 unsigned char batt
, ctrl
;
797 spin_lock_irq(&rtc_lock
);
798 batt
= CMOS_READ(RTC_VALID
) & RTC_VRT
;
799 ctrl
= CMOS_READ(RTC_CONTROL
);
801 spin_unlock_irq(&rtc_lock
);
808 * There is no way to tell if the luser has the RTC set for local
809 * time or for Universal Standard Time (GMT). Probably local though.
812 "rtc_time\t: %02d:%02d:%02d\n"
813 "rtc_date\t: %04d-%02d-%02d\n"
814 "rtc_epoch\t: %04lu\n",
815 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
,
816 tm
.tm_year
+ 1900, tm
.tm_mon
+ 1, tm
.tm_mday
, epoch
);
818 get_rtc_alm_time(&tm
);
821 * We implicitly assume 24hr mode here. Alarm values >= 0xc0 will
822 * match any value for that particular field. Values that are
823 * greater than a valid time, but less than 0xc0 shouldn't appear.
825 p
+= sprintf(p
, "alarm\t\t: ");
826 if (tm
.tm_hour
<= 24)
827 p
+= sprintf(p
, "%02d:", tm
.tm_hour
);
829 p
+= sprintf(p
, "**:");
832 p
+= sprintf(p
, "%02d:", tm
.tm_min
);
834 p
+= sprintf(p
, "**:");
837 p
+= sprintf(p
, "%02d\n", tm
.tm_sec
);
839 p
+= sprintf(p
, "**\n");
845 "square_wave\t: %s\n"
848 "periodic_IRQ\t: %s\n"
849 "periodic_freq\t: %ld\n"
850 "batt_status\t: %s\n",
859 batt
? "okay" : "dead");
866 static int rtc_read_proc(char *page
, char **start
, off_t off
,
867 int count
, int *eof
, void *data
)
869 int len
= rtc_proc_output (page
);
870 if (len
<= off
+count
) *eof
= 1;
873 if (len
>count
) len
= count
;
879 * Returns true if a clock update is in progress
881 /* FIXME shouldn't this be above rtc_init to make it fully inlined? */
882 static inline unsigned char rtc_is_updating(void)
886 spin_lock_irq(&rtc_lock
);
887 uip
= (CMOS_READ(RTC_FREQ_SELECT
) & RTC_UIP
);
888 spin_unlock_irq(&rtc_lock
);
892 static void get_rtc_time(struct rtc_time
*rtc_tm
)
894 unsigned long uip_watchdog
= jiffies
;
898 * read RTC once any update in progress is done. The update
899 * can take just over 2ms. We wait 10 to 20ms. There is no need to
900 * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
901 * If you need to know *exactly* when a second has started, enable
902 * periodic update complete interrupts, (via ioctl) and then
903 * immediately read /dev/rtc which will block until you get the IRQ.
904 * Once the read clears, read the RTC time (again via ioctl). Easy.
907 if (rtc_is_updating() != 0)
908 while (jiffies
- uip_watchdog
< 2*HZ
/100)
912 * Only the values that we read from the RTC are set. We leave
913 * tm_wday, tm_yday and tm_isdst untouched. Even though the
914 * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
915 * by the RTC when initially set to a non-zero value.
917 spin_lock_irq(&rtc_lock
);
918 rtc_tm
->tm_sec
= CMOS_READ(RTC_SECONDS
);
919 rtc_tm
->tm_min
= CMOS_READ(RTC_MINUTES
);
920 rtc_tm
->tm_hour
= CMOS_READ(RTC_HOURS
);
921 rtc_tm
->tm_mday
= CMOS_READ(RTC_DAY_OF_MONTH
);
922 rtc_tm
->tm_mon
= CMOS_READ(RTC_MONTH
);
923 rtc_tm
->tm_year
= CMOS_READ(RTC_YEAR
);
924 ctrl
= CMOS_READ(RTC_CONTROL
);
925 spin_unlock_irq(&rtc_lock
);
927 if (!(ctrl
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
)
929 BCD_TO_BIN(rtc_tm
->tm_sec
);
930 BCD_TO_BIN(rtc_tm
->tm_min
);
931 BCD_TO_BIN(rtc_tm
->tm_hour
);
932 BCD_TO_BIN(rtc_tm
->tm_mday
);
933 BCD_TO_BIN(rtc_tm
->tm_mon
);
934 BCD_TO_BIN(rtc_tm
->tm_year
);
938 * Account for differences between how the RTC uses the values
939 * and how they are defined in a struct rtc_time;
941 if ((rtc_tm
->tm_year
+= (epoch
- 1900)) <= 69)
942 rtc_tm
->tm_year
+= 100;
947 static void get_rtc_alm_time(struct rtc_time
*alm_tm
)
952 * Only the values that we read from the RTC are set. That
953 * means only tm_hour, tm_min, and tm_sec.
955 spin_lock_irq(&rtc_lock
);
956 alm_tm
->tm_sec
= CMOS_READ(RTC_SECONDS_ALARM
);
957 alm_tm
->tm_min
= CMOS_READ(RTC_MINUTES_ALARM
);
958 alm_tm
->tm_hour
= CMOS_READ(RTC_HOURS_ALARM
);
959 ctrl
= CMOS_READ(RTC_CONTROL
);
960 spin_unlock_irq(&rtc_lock
);
962 if (!(ctrl
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
)
964 BCD_TO_BIN(alm_tm
->tm_sec
);
965 BCD_TO_BIN(alm_tm
->tm_min
);
966 BCD_TO_BIN(alm_tm
->tm_hour
);
972 * Used to disable/enable interrupts for any one of UIE, AIE, PIE.
973 * Rumour has it that if you frob the interrupt enable/disable
974 * bits in RTC_CONTROL, you should read RTC_INTR_FLAGS, to
975 * ensure you actually start getting interrupts. Probably for
976 * compatibility with older/broken chipset RTC implementations.
977 * We also clear out any old irq data after an ioctl() that
978 * meddles with the interrupt enable/disable bits.
981 static void mask_rtc_irq_bit(unsigned char bit
)
985 spin_lock_irq(&rtc_lock
);
986 val
= CMOS_READ(RTC_CONTROL
);
988 CMOS_WRITE(val
, RTC_CONTROL
);
989 CMOS_READ(RTC_INTR_FLAGS
);
992 spin_unlock_irq(&rtc_lock
);
995 static void set_rtc_irq_bit(unsigned char bit
)
999 spin_lock_irq(&rtc_lock
);
1000 val
= CMOS_READ(RTC_CONTROL
);
1002 CMOS_WRITE(val
, RTC_CONTROL
);
1003 CMOS_READ(RTC_INTR_FLAGS
);
1006 spin_unlock_irq(&rtc_lock
);