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_IO_EXTENT 0x10 /* Only really two ports, but... */
49 * Note that *all* calls to CMOS_READ and CMOS_WRITE are done with
50 * interrupts disabled. Due to the index-port/data-port (0x70/0x71)
51 * design of the RTC, we don't want two different things trying to
52 * get to it at once. (e.g. the periodic 11 min sync from time.c vs.
56 #include <linux/module.h>
57 #include <linux/kernel.h>
58 #include <linux/types.h>
59 #include <linux/miscdevice.h>
60 #include <linux/ioport.h>
61 #include <linux/fcntl.h>
62 #include <linux/mc146818rtc.h>
63 #include <linux/init.h>
64 #include <linux/poll.h>
65 #include <linux/proc_fs.h>
66 #include <linux/spinlock.h>
69 #include <asm/uaccess.h>
70 #include <asm/system.h>
75 static unsigned long rtc_port
;
80 * We sponge a minor off of the misc major. No need slurping
81 * up another valuable major dev number for this. If you add
82 * an ioctl, make sure you don't conflict with SPARC's RTC
86 static struct fasync_struct
*rtc_async_queue
;
88 static DECLARE_WAIT_QUEUE_HEAD(rtc_wait
);
90 extern spinlock_t rtc_lock
;
92 static struct timer_list rtc_irq_timer
;
94 static loff_t
rtc_llseek(struct file
*file
, loff_t offset
, int origin
);
96 static ssize_t
rtc_read(struct file
*file
, char *buf
,
97 size_t count
, loff_t
*ppos
);
99 static int rtc_ioctl(struct inode
*inode
, struct file
*file
,
100 unsigned int cmd
, unsigned long arg
);
103 static unsigned int rtc_poll(struct file
*file
, poll_table
*wait
);
106 static void get_rtc_time (struct rtc_time
*rtc_tm
);
107 static void get_rtc_alm_time (struct rtc_time
*alm_tm
);
109 static void rtc_dropped_irq(unsigned long data
);
111 static void set_rtc_irq_bit(unsigned char bit
);
112 static void mask_rtc_irq_bit(unsigned char bit
);
115 static inline unsigned char rtc_is_updating(void);
117 static int rtc_read_proc(char *page
, char **start
, off_t off
,
118 int count
, int *eof
, void *data
);
121 * Bits in rtc_status. (6 bits of room for future expansion)
124 #define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
125 #define RTC_TIMER_ON 0x02 /* missed irq timer active */
128 * rtc_status is never changed by rtc_interrupt, and ioctl/open/close is
129 * protected by the big kernel lock. However, ioctl can still disable the timer
130 * in rtc_status and then with del_timer after the interrupt has read
131 * rtc_status but before mod_timer is called, which would then reenable the
132 * timer (but you would need to have an awful timing before you'd trip on it)
134 static unsigned long rtc_status
= 0; /* bitmapped status byte. */
135 static unsigned long rtc_freq
= 0; /* Current periodic IRQ rate */
136 static unsigned long rtc_irq_data
= 0; /* our output to the world */
139 * If this driver ever becomes modularised, it will be really nice
140 * to make the epoch retain its value across module reload...
143 static unsigned long epoch
= 1900; /* year corresponding to 0x00 */
145 static const unsigned char days_in_mo
[] =
146 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
150 * A very tiny interrupt handler. It runs with SA_INTERRUPT set,
151 * but there is possibility of conflicting with the set_rtc_mmss()
152 * call (the rtc irq and the timer irq can easily run at the same
153 * time in two different CPUs). So we need to serializes
154 * accesses to the chip with the rtc_lock spinlock that each
155 * architecture should implement in the timer code.
156 * (See ./arch/XXXX/kernel/time.c for the set_rtc_mmss() function.)
159 static void rtc_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
162 * Can be an alarm interrupt, update complete interrupt,
163 * or a periodic interrupt. We store the status in the
164 * low byte and the number of interrupts received since
165 * the last read in the remainder of rtc_irq_data.
168 spin_lock (&rtc_lock
);
169 rtc_irq_data
+= 0x100;
170 rtc_irq_data
&= ~0xff;
171 rtc_irq_data
|= (CMOS_READ(RTC_INTR_FLAGS
) & 0xF0);
173 if (rtc_status
& RTC_TIMER_ON
)
174 mod_timer(&rtc_irq_timer
, jiffies
+ HZ
/rtc_freq
+ 2*HZ
/100);
176 spin_unlock (&rtc_lock
);
178 /* Now do the rest of the actions */
179 wake_up_interruptible(&rtc_wait
);
181 kill_fasync (&rtc_async_queue
, SIGIO
, POLL_IN
);
186 * Now all the various file operations that we export.
189 static loff_t
rtc_llseek(struct file
*file
, loff_t offset
, int origin
)
194 static ssize_t
rtc_read(struct file
*file
, char *buf
,
195 size_t count
, loff_t
*ppos
)
200 DECLARE_WAITQUEUE(wait
, current
);
204 if (count
< sizeof(unsigned long))
207 add_wait_queue(&rtc_wait
, &wait
);
209 current
->state
= TASK_INTERRUPTIBLE
;
212 /* First make it right. Then make it fast. Putting this whole
213 * block within the parentheses of a while would be too
214 * confusing. And no, xchg() is not the answer. */
215 spin_lock_irq (&rtc_lock
);
218 spin_unlock_irq (&rtc_lock
);
223 if (file
->f_flags
& O_NONBLOCK
) {
227 if (signal_pending(current
)) {
228 retval
= -ERESTARTSYS
;
234 retval
= put_user(data
, (unsigned long *)buf
);
236 retval
= sizeof(unsigned long);
238 current
->state
= TASK_RUNNING
;
239 remove_wait_queue(&rtc_wait
, &wait
);
245 static int rtc_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
,
248 struct rtc_time wtime
;
252 case RTC_AIE_OFF
: /* Mask alarm int. enab. bit */
254 mask_rtc_irq_bit(RTC_AIE
);
257 case RTC_AIE_ON
: /* Allow alarm interrupts. */
259 set_rtc_irq_bit(RTC_AIE
);
262 case RTC_PIE_OFF
: /* Mask periodic int. enab. bit */
264 mask_rtc_irq_bit(RTC_PIE
);
265 if (rtc_status
& RTC_TIMER_ON
) {
266 spin_lock_irq (&rtc_lock
);
267 rtc_status
&= ~RTC_TIMER_ON
;
268 del_timer(&rtc_irq_timer
);
269 spin_unlock_irq (&rtc_lock
);
273 case RTC_PIE_ON
: /* Allow periodic ints */
277 * We don't really want Joe User enabling more
278 * than 64Hz of interrupts on a multi-user machine.
280 if ((rtc_freq
> 64) && (!capable(CAP_SYS_RESOURCE
)))
283 if (!(rtc_status
& RTC_TIMER_ON
)) {
284 spin_lock_irq (&rtc_lock
);
285 rtc_irq_timer
.expires
= jiffies
+ HZ
/rtc_freq
+ 2*HZ
/100;
286 add_timer(&rtc_irq_timer
);
287 rtc_status
|= RTC_TIMER_ON
;
288 spin_unlock_irq (&rtc_lock
);
290 set_rtc_irq_bit(RTC_PIE
);
293 case RTC_UIE_OFF
: /* Mask ints from RTC updates. */
295 mask_rtc_irq_bit(RTC_UIE
);
298 case RTC_UIE_ON
: /* Allow ints for RTC updates. */
300 set_rtc_irq_bit(RTC_UIE
);
304 case RTC_ALM_READ
: /* Read the present alarm time */
307 * This returns a struct rtc_time. Reading >= 0xc0
308 * means "don't care" or "match all". Only the tm_hour,
309 * tm_min, and tm_sec values are filled in.
312 get_rtc_alm_time(&wtime
);
315 case RTC_ALM_SET
: /* Store a time into the alarm */
318 * This expects a struct rtc_time. Writing 0xff means
319 * "don't care" or "match all". Only the tm_hour,
320 * tm_min and tm_sec are used.
322 unsigned char hrs
, min
, sec
;
323 struct rtc_time alm_tm
;
325 if (copy_from_user(&alm_tm
, (struct rtc_time
*)arg
,
326 sizeof(struct rtc_time
)))
329 hrs
= alm_tm
.tm_hour
;
342 spin_lock_irq(&rtc_lock
);
343 if (!(CMOS_READ(RTC_CONTROL
) & RTC_DM_BINARY
) ||
350 CMOS_WRITE(hrs
, RTC_HOURS_ALARM
);
351 CMOS_WRITE(min
, RTC_MINUTES_ALARM
);
352 CMOS_WRITE(sec
, RTC_SECONDS_ALARM
);
353 spin_unlock_irq(&rtc_lock
);
357 case RTC_RD_TIME
: /* Read the time/date from RTC */
359 get_rtc_time(&wtime
);
362 case RTC_SET_TIME
: /* Set the RTC */
364 struct rtc_time rtc_tm
;
365 unsigned char mon
, day
, hrs
, min
, sec
, leap_yr
;
366 unsigned char save_control
, save_freq_select
;
369 if (!capable(CAP_SYS_TIME
))
372 if (copy_from_user(&rtc_tm
, (struct rtc_time
*)arg
,
373 sizeof(struct rtc_time
)))
376 yrs
= rtc_tm
.tm_year
+ 1900;
377 mon
= rtc_tm
.tm_mon
+ 1; /* tm_mon starts at zero */
378 day
= rtc_tm
.tm_mday
;
379 hrs
= rtc_tm
.tm_hour
;
386 leap_yr
= ((!(yrs
% 4) && (yrs
% 100)) || !(yrs
% 400));
388 if ((mon
> 12) || (day
== 0))
391 if (day
> (days_in_mo
[mon
] + ((mon
== 2) && leap_yr
)))
394 if ((hrs
>= 24) || (min
>= 60) || (sec
>= 60))
397 if ((yrs
-= epoch
) > 255) /* They are unsigned */
400 spin_lock_irq(&rtc_lock
);
401 if (!(CMOS_READ(RTC_CONTROL
) & RTC_DM_BINARY
)
404 spin_unlock_irq(&rtc_lock
);
418 save_control
= CMOS_READ(RTC_CONTROL
);
419 CMOS_WRITE((save_control
|RTC_SET
), RTC_CONTROL
);
420 save_freq_select
= CMOS_READ(RTC_FREQ_SELECT
);
421 CMOS_WRITE((save_freq_select
|RTC_DIV_RESET2
), RTC_FREQ_SELECT
);
423 CMOS_WRITE(yrs
, RTC_YEAR
);
424 CMOS_WRITE(mon
, RTC_MONTH
);
425 CMOS_WRITE(day
, RTC_DAY_OF_MONTH
);
426 CMOS_WRITE(hrs
, RTC_HOURS
);
427 CMOS_WRITE(min
, RTC_MINUTES
);
428 CMOS_WRITE(sec
, RTC_SECONDS
);
430 CMOS_WRITE(save_control
, RTC_CONTROL
);
431 CMOS_WRITE(save_freq_select
, RTC_FREQ_SELECT
);
433 spin_unlock_irq(&rtc_lock
);
437 case RTC_IRQP_READ
: /* Read the periodic IRQ rate. */
439 return put_user(rtc_freq
, (unsigned long *)arg
);
441 case RTC_IRQP_SET
: /* Set periodic IRQ rate. */
447 * The max we can do is 8192Hz.
449 if ((arg
< 2) || (arg
> 8192))
452 * We don't really want Joe User generating more
453 * than 64Hz of interrupts on a multi-user machine.
455 if ((arg
> 64) && (!capable(CAP_SYS_RESOURCE
)))
458 while (arg
> (1<<tmp
))
462 * Check that the input was really a power of 2.
467 spin_lock_irq(&rtc_lock
);
470 val
= CMOS_READ(RTC_FREQ_SELECT
) & 0xf0;
472 CMOS_WRITE(val
, RTC_FREQ_SELECT
);
473 spin_unlock_irq(&rtc_lock
);
476 #elif !defined(CONFIG_DECSTATION)
477 case RTC_EPOCH_READ
: /* Read the epoch. */
479 return put_user (epoch
, (unsigned long *)arg
);
481 case RTC_EPOCH_SET
: /* Set the epoch. */
484 * There were no RTC clocks before 1900.
489 if (!capable(CAP_SYS_TIME
))
499 return copy_to_user((void *)arg
, &wtime
, sizeof wtime
) ? -EFAULT
: 0;
503 * We enforce only one user at a time here with the open/close.
504 * Also clear the previous interrupt data on an open, and clean
505 * up things on a close.
508 /* We use rtc_lock to protect against concurrent opens. So the BKL is not
509 * needed here. Or anywhere else in this driver. */
510 static int rtc_open(struct inode
*inode
, struct file
*file
)
512 spin_lock_irq (&rtc_lock
);
514 if(rtc_status
& RTC_IS_OPEN
)
517 rtc_status
|= RTC_IS_OPEN
;
520 spin_unlock_irq (&rtc_lock
);
524 spin_unlock_irq (&rtc_lock
);
528 static int rtc_fasync (int fd
, struct file
*filp
, int on
)
531 return fasync_helper (fd
, filp
, on
, &rtc_async_queue
);
534 static int rtc_release(struct inode
*inode
, struct file
*file
)
538 * Turn off all interrupts once the device is no longer
539 * in use, and clear the data.
544 spin_lock_irq(&rtc_lock
);
545 tmp
= CMOS_READ(RTC_CONTROL
);
549 CMOS_WRITE(tmp
, RTC_CONTROL
);
550 CMOS_READ(RTC_INTR_FLAGS
);
552 if (rtc_status
& RTC_TIMER_ON
) {
553 rtc_status
&= ~RTC_TIMER_ON
;
554 del_timer(&rtc_irq_timer
);
556 spin_unlock_irq(&rtc_lock
);
558 if (file
->f_flags
& FASYNC
) {
559 rtc_fasync (-1, file
, 0);
563 spin_lock_irq (&rtc_lock
);
565 spin_unlock_irq (&rtc_lock
);
567 /* No need for locking -- nobody else can do anything until this rmw is
568 * committed, and no timer is running. */
569 rtc_status
&= ~RTC_IS_OPEN
;
574 /* Called without the kernel lock - fine */
575 static unsigned int rtc_poll(struct file
*file
, poll_table
*wait
)
579 poll_wait(file
, &rtc_wait
, wait
);
581 spin_lock_irq (&rtc_lock
);
583 spin_unlock_irq (&rtc_lock
);
586 return POLLIN
| POLLRDNORM
;
592 * The various file operations we support.
595 static struct file_operations rtc_fops
= {
604 release
: rtc_release
,
608 static struct miscdevice rtc_dev
=
615 static int __init
rtc_init(void)
617 #if defined(__alpha__) || defined(__mips__)
618 unsigned int year
, ctrl
;
619 unsigned long uip_watchdog
;
623 struct linux_ebus
*ebus
;
624 struct linux_ebus_device
*edev
;
628 for_each_ebus(ebus
) {
629 for_each_ebusdev(edev
, ebus
) {
630 if(strcmp(edev
->prom_name
, "rtc") == 0) {
635 printk("rtc_init: no PC rtc found\n");
639 rtc_port
= edev
->resource
[0].start
;
640 rtc_irq
= edev
->irqs
[0];
642 * XXX Interrupt pin #7 in Espresso is shared between RTC and
643 * PCI Slot 2 INTA# (and some INTx# in Slot 1). SA_INTERRUPT here
644 * is asking for trouble with add-on boards. Change to SA_SHIRQ.
646 if(request_irq(rtc_irq
, rtc_interrupt
, SA_INTERRUPT
, "rtc", (void *)&rtc_port
)) {
648 * Standard way for sparc to print irq's is to use
649 * __irq_itoa(). I think for EBus it's ok to use %d.
651 printk("rtc: cannot register IRQ %d\n", rtc_irq
);
655 if (check_region (RTC_PORT (0), RTC_IO_EXTENT
))
657 printk(KERN_ERR
"rtc: I/O port %d is not free.\n", RTC_PORT (0));
662 if(request_irq(RTC_IRQ
, rtc_interrupt
, SA_INTERRUPT
, "rtc", NULL
))
664 /* Yeah right, seeing as irq 8 doesn't even hit the bus. */
665 printk(KERN_ERR
"rtc: IRQ %d is not free.\n", RTC_IRQ
);
670 request_region(RTC_PORT(0), RTC_IO_EXTENT
, "rtc");
671 #endif /* __sparc__ vs. others */
673 misc_register(&rtc_dev
);
674 create_proc_read_entry ("driver/rtc", 0, 0, rtc_read_proc
, NULL
);
676 #if defined(__alpha__) || defined(__mips__)
679 /* Each operating system on an Alpha uses its own epoch.
680 Let's try to guess which one we are using now. */
682 uip_watchdog
= jiffies
;
683 if (rtc_is_updating() != 0)
684 while (jiffies
- uip_watchdog
< 2*HZ
/100)
687 spin_lock_irq(&rtc_lock
);
688 year
= CMOS_READ(RTC_YEAR
);
689 ctrl
= CMOS_READ(RTC_CONTROL
);
690 spin_unlock_irq(&rtc_lock
);
692 if (!(ctrl
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
)
693 BCD_TO_BIN(year
); /* This should never happen... */
695 if (year
> 20 && year
< 48) {
697 guess
= "ARC console";
698 } else if (year
>= 48 && year
< 70) {
700 guess
= "Digital UNIX";
701 } else if (year
>= 70 && year
< 100) {
703 guess
= "Digital DECstation";
706 printk("rtc: %s epoch (%lu) detected\n", guess
, epoch
);
709 init_timer(&rtc_irq_timer
);
710 rtc_irq_timer
.function
= rtc_dropped_irq
;
711 spin_lock_irq(&rtc_lock
);
712 /* Initialize periodic freq. to CMOS reset default, which is 1024Hz */
713 CMOS_WRITE(((CMOS_READ(RTC_FREQ_SELECT
) & 0xF0) | 0x06), RTC_FREQ_SELECT
);
714 spin_unlock_irq(&rtc_lock
);
718 printk(KERN_INFO
"Real Time Clock Driver v" RTC_VERSION
"\n");
723 static void __exit
rtc_exit (void)
725 /* interrupts and maybe timer disabled at this point by rtc_release */
726 /* FIXME: Maybe??? */
728 if (rtc_status
& RTC_TIMER_ON
) {
729 spin_lock_irq (&rtc_lock
);
730 rtc_status
&= ~RTC_TIMER_ON
;
731 del_timer(&rtc_irq_timer
);
732 spin_unlock_irq (&rtc_lock
);
734 printk(KERN_WARNING
"rtc_exit(), and timer still running.\n");
737 remove_proc_entry ("driver/rtc", NULL
);
738 misc_deregister(&rtc_dev
);
741 free_irq (rtc_irq
, &rtc_port
);
743 release_region (RTC_PORT (0), RTC_IO_EXTENT
);
745 free_irq (RTC_IRQ
, NULL
);
747 #endif /* __sparc__ */
750 module_init(rtc_init
);
751 module_exit(rtc_exit
);
756 * At IRQ rates >= 4096Hz, an interrupt may get lost altogether.
757 * (usually during an IDE disk interrupt, with IRQ unmasking off)
758 * Since the interrupt handler doesn't get called, the IRQ status
759 * byte doesn't get read, and the RTC stops generating interrupts.
760 * A timer is set, and will call this function if/when that happens.
761 * To get it out of this stalled state, we just read the status.
762 * At least a jiffy of interrupts (rtc_freq/HZ) will have been lost.
763 * (You *really* shouldn't be trying to use a non-realtime system
764 * for something that requires a steady > 1KHz signal anyways.)
767 static void rtc_dropped_irq(unsigned long data
)
771 spin_lock_irq (&rtc_lock
);
773 /* Just in case someone disabled the timer from behind our back... */
774 if (rtc_status
& RTC_TIMER_ON
)
775 mod_timer(&rtc_irq_timer
, jiffies
+ HZ
/rtc_freq
+ 2*HZ
/100);
777 rtc_irq_data
+= ((rtc_freq
/HZ
)<<8);
778 rtc_irq_data
&= ~0xff;
779 rtc_irq_data
|= (CMOS_READ(RTC_INTR_FLAGS
) & 0xF0); /* restart */
783 spin_unlock_irq(&rtc_lock
);
785 printk(KERN_INFO
"rtc: lost some interrupts at %ldHz.\n", freq
);
787 /* Now we have new data */
788 wake_up_interruptible(&rtc_wait
);
790 kill_fasync (&rtc_async_queue
, SIGIO
, POLL_IN
);
795 * Info exported via "/proc/driver/rtc".
798 static int rtc_proc_output (char *buf
)
800 #define YN(bit) ((ctrl & bit) ? "yes" : "no")
801 #define NY(bit) ((ctrl & bit) ? "no" : "yes")
804 unsigned char batt
, ctrl
;
807 spin_lock_irq(&rtc_lock
);
808 batt
= CMOS_READ(RTC_VALID
) & RTC_VRT
;
809 ctrl
= CMOS_READ(RTC_CONTROL
);
811 spin_unlock_irq(&rtc_lock
);
818 * There is no way to tell if the luser has the RTC set for local
819 * time or for Universal Standard Time (GMT). Probably local though.
822 "rtc_time\t: %02d:%02d:%02d\n"
823 "rtc_date\t: %04d-%02d-%02d\n"
824 "rtc_epoch\t: %04lu\n",
825 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
,
826 tm
.tm_year
+ 1900, tm
.tm_mon
+ 1, tm
.tm_mday
, epoch
);
828 get_rtc_alm_time(&tm
);
831 * We implicitly assume 24hr mode here. Alarm values >= 0xc0 will
832 * match any value for that particular field. Values that are
833 * greater than a valid time, but less than 0xc0 shouldn't appear.
835 p
+= sprintf(p
, "alarm\t\t: ");
836 if (tm
.tm_hour
<= 24)
837 p
+= sprintf(p
, "%02d:", tm
.tm_hour
);
839 p
+= sprintf(p
, "**:");
842 p
+= sprintf(p
, "%02d:", tm
.tm_min
);
844 p
+= sprintf(p
, "**:");
847 p
+= sprintf(p
, "%02d\n", tm
.tm_sec
);
849 p
+= sprintf(p
, "**\n");
855 "square_wave\t: %s\n"
858 "periodic_IRQ\t: %s\n"
859 "periodic_freq\t: %ld\n"
860 "batt_status\t: %s\n",
869 batt
? "okay" : "dead");
876 static int rtc_read_proc(char *page
, char **start
, off_t off
,
877 int count
, int *eof
, void *data
)
879 int len
= rtc_proc_output (page
);
880 if (len
<= off
+count
) *eof
= 1;
883 if (len
>count
) len
= count
;
889 * Returns true if a clock update is in progress
891 /* FIXME shouldn't this be above rtc_init to make it fully inlined? */
892 static inline unsigned char rtc_is_updating(void)
896 spin_lock_irq(&rtc_lock
);
897 uip
= (CMOS_READ(RTC_FREQ_SELECT
) & RTC_UIP
);
898 spin_unlock_irq(&rtc_lock
);
902 static void get_rtc_time(struct rtc_time
*rtc_tm
)
904 unsigned long uip_watchdog
= jiffies
;
908 * read RTC once any update in progress is done. The update
909 * can take just over 2ms. We wait 10 to 20ms. There is no need to
910 * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
911 * If you need to know *exactly* when a second has started, enable
912 * periodic update complete interrupts, (via ioctl) and then
913 * immediately read /dev/rtc which will block until you get the IRQ.
914 * Once the read clears, read the RTC time (again via ioctl). Easy.
917 if (rtc_is_updating() != 0)
918 while (jiffies
- uip_watchdog
< 2*HZ
/100)
922 * Only the values that we read from the RTC are set. We leave
923 * tm_wday, tm_yday and tm_isdst untouched. Even though the
924 * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
925 * by the RTC when initially set to a non-zero value.
927 spin_lock_irq(&rtc_lock
);
928 rtc_tm
->tm_sec
= CMOS_READ(RTC_SECONDS
);
929 rtc_tm
->tm_min
= CMOS_READ(RTC_MINUTES
);
930 rtc_tm
->tm_hour
= CMOS_READ(RTC_HOURS
);
931 rtc_tm
->tm_mday
= CMOS_READ(RTC_DAY_OF_MONTH
);
932 rtc_tm
->tm_mon
= CMOS_READ(RTC_MONTH
);
933 rtc_tm
->tm_year
= CMOS_READ(RTC_YEAR
);
934 ctrl
= CMOS_READ(RTC_CONTROL
);
935 spin_unlock_irq(&rtc_lock
);
937 if (!(ctrl
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
)
939 BCD_TO_BIN(rtc_tm
->tm_sec
);
940 BCD_TO_BIN(rtc_tm
->tm_min
);
941 BCD_TO_BIN(rtc_tm
->tm_hour
);
942 BCD_TO_BIN(rtc_tm
->tm_mday
);
943 BCD_TO_BIN(rtc_tm
->tm_mon
);
944 BCD_TO_BIN(rtc_tm
->tm_year
);
948 * Account for differences between how the RTC uses the values
949 * and how they are defined in a struct rtc_time;
951 if ((rtc_tm
->tm_year
+= (epoch
- 1900)) <= 69)
952 rtc_tm
->tm_year
+= 100;
957 static void get_rtc_alm_time(struct rtc_time
*alm_tm
)
962 * Only the values that we read from the RTC are set. That
963 * means only tm_hour, tm_min, and tm_sec.
965 spin_lock_irq(&rtc_lock
);
966 alm_tm
->tm_sec
= CMOS_READ(RTC_SECONDS_ALARM
);
967 alm_tm
->tm_min
= CMOS_READ(RTC_MINUTES_ALARM
);
968 alm_tm
->tm_hour
= CMOS_READ(RTC_HOURS_ALARM
);
969 ctrl
= CMOS_READ(RTC_CONTROL
);
970 spin_unlock_irq(&rtc_lock
);
972 if (!(ctrl
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
)
974 BCD_TO_BIN(alm_tm
->tm_sec
);
975 BCD_TO_BIN(alm_tm
->tm_min
);
976 BCD_TO_BIN(alm_tm
->tm_hour
);
982 * Used to disable/enable interrupts for any one of UIE, AIE, PIE.
983 * Rumour has it that if you frob the interrupt enable/disable
984 * bits in RTC_CONTROL, you should read RTC_INTR_FLAGS, to
985 * ensure you actually start getting interrupts. Probably for
986 * compatibility with older/broken chipset RTC implementations.
987 * We also clear out any old irq data after an ioctl() that
988 * meddles with the interrupt enable/disable bits.
991 static void mask_rtc_irq_bit(unsigned char bit
)
995 spin_lock_irq(&rtc_lock
);
996 val
= CMOS_READ(RTC_CONTROL
);
998 CMOS_WRITE(val
, RTC_CONTROL
);
999 CMOS_READ(RTC_INTR_FLAGS
);
1002 spin_unlock_irq(&rtc_lock
);
1005 static void set_rtc_irq_bit(unsigned char bit
)
1009 spin_lock_irq(&rtc_lock
);
1010 val
= CMOS_READ(RTC_CONTROL
);
1012 CMOS_WRITE(val
, RTC_CONTROL
);
1013 CMOS_READ(RTC_INTR_FLAGS
);
1016 spin_unlock_irq(&rtc_lock
);