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
52 #define RTC_VERSION "1.12"
54 #define RTC_IO_EXTENT 0x8
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 time.c vs.
64 #include <linux/config.h>
65 #include <linux/interrupt.h>
66 #include <linux/module.h>
67 #include <linux/kernel.h>
68 #include <linux/types.h>
69 #include <linux/miscdevice.h>
70 #include <linux/ioport.h>
71 #include <linux/fcntl.h>
72 #include <linux/mc146818rtc.h>
73 #include <linux/init.h>
74 #include <linux/poll.h>
75 #include <linux/proc_fs.h>
76 #include <linux/spinlock.h>
77 #include <linux/sysctl.h>
78 #include <linux/wait.h>
79 #include <linux/bcd.h>
81 #include <asm/current.h>
82 #include <asm/uaccess.h>
83 #include <asm/system.h>
90 #include <linux/pci.h>
96 static unsigned long rtc_port
;
97 static int rtc_irq
= PCI_IRQ_NONE
;
101 static int rtc_has_irq
= 1;
104 #ifndef CONFIG_HPET_EMULATE_RTC
105 #define is_hpet_enabled() 0
106 #define hpet_set_alarm_time(hrs, min, sec) 0
107 #define hpet_set_periodic_freq(arg) 0
108 #define hpet_mask_rtc_irq_bit(arg) 0
109 #define hpet_set_rtc_irq_bit(arg) 0
110 #define hpet_rtc_timer_init() do { } while (0)
111 #define hpet_rtc_dropped_irq() 0
112 static inline irqreturn_t
hpet_rtc_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
) {return 0;}
116 * We sponge a minor off of the misc major. No need slurping
117 * up another valuable major dev number for this. If you add
118 * an ioctl, make sure you don't conflict with SPARC's RTC
122 static struct fasync_struct
*rtc_async_queue
;
124 static DECLARE_WAIT_QUEUE_HEAD(rtc_wait
);
127 static struct timer_list rtc_irq_timer
;
130 static ssize_t
rtc_read(struct file
*file
, char *buf
,
131 size_t count
, loff_t
*ppos
);
133 static int rtc_ioctl(struct inode
*inode
, struct file
*file
,
134 unsigned int cmd
, unsigned long arg
);
137 static unsigned int rtc_poll(struct file
*file
, poll_table
*wait
);
140 static void get_rtc_alm_time (struct rtc_time
*alm_tm
);
142 static void rtc_dropped_irq(unsigned long data
);
144 static void set_rtc_irq_bit(unsigned char bit
);
145 static void mask_rtc_irq_bit(unsigned char bit
);
148 static inline unsigned char rtc_is_updating(void);
150 static int rtc_read_proc(char *page
, char **start
, off_t off
,
151 int count
, int *eof
, void *data
);
154 * Bits in rtc_status. (6 bits of room for future expansion)
157 #define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
158 #define RTC_TIMER_ON 0x02 /* missed irq timer active */
161 * rtc_status is never changed by rtc_interrupt, and ioctl/open/close is
162 * protected by the big kernel lock. However, ioctl can still disable the timer
163 * in rtc_status and then with del_timer after the interrupt has read
164 * rtc_status but before mod_timer is called, which would then reenable the
165 * timer (but you would need to have an awful timing before you'd trip on it)
167 static unsigned long rtc_status
= 0; /* bitmapped status byte. */
168 static unsigned long rtc_freq
= 0; /* Current periodic IRQ rate */
169 static unsigned long rtc_irq_data
= 0; /* our output to the world */
170 static unsigned long rtc_max_user_freq
= 64; /* > this, need CAP_SYS_RESOURCE */
174 * rtc_task_lock nests inside rtc_lock.
176 static spinlock_t rtc_task_lock
= SPIN_LOCK_UNLOCKED
;
177 static rtc_task_t
*rtc_callback
= NULL
;
181 * If this driver ever becomes modularised, it will be really nice
182 * to make the epoch retain its value across module reload...
185 static unsigned long epoch
= 1900; /* year corresponding to 0x00 */
187 static const unsigned char days_in_mo
[] =
188 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
192 * A very tiny interrupt handler. It runs with SA_INTERRUPT set,
193 * but there is possibility of conflicting with the set_rtc_mmss()
194 * call (the rtc irq and the timer irq can easily run at the same
195 * time in two different CPUs). So we need to serialize
196 * accesses to the chip with the rtc_lock spinlock that each
197 * architecture should implement in the timer code.
198 * (See ./arch/XXXX/kernel/time.c for the set_rtc_mmss() function.)
201 irqreturn_t
rtc_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
204 * Can be an alarm interrupt, update complete interrupt,
205 * or a periodic interrupt. We store the status in the
206 * low byte and the number of interrupts received since
207 * the last read in the remainder of rtc_irq_data.
210 spin_lock (&rtc_lock
);
211 rtc_irq_data
+= 0x100;
212 rtc_irq_data
&= ~0xff;
213 if (is_hpet_enabled()) {
215 * In this case it is HPET RTC interrupt handler
216 * calling us, with the interrupt information
217 * passed as arg1, instead of irq.
219 rtc_irq_data
|= (unsigned long)irq
& 0xF0;
221 rtc_irq_data
|= (CMOS_READ(RTC_INTR_FLAGS
) & 0xF0);
224 if (rtc_status
& RTC_TIMER_ON
)
225 mod_timer(&rtc_irq_timer
, jiffies
+ HZ
/rtc_freq
+ 2*HZ
/100);
227 spin_unlock (&rtc_lock
);
229 /* Now do the rest of the actions */
230 spin_lock(&rtc_task_lock
);
232 rtc_callback
->func(rtc_callback
->private_data
);
233 spin_unlock(&rtc_task_lock
);
234 wake_up_interruptible(&rtc_wait
);
236 kill_fasync (&rtc_async_queue
, SIGIO
, POLL_IN
);
243 * sysctl-tuning infrastructure.
245 static ctl_table rtc_table
[] = {
248 .procname
= "max-user-freq",
249 .data
= &rtc_max_user_freq
,
250 .maxlen
= sizeof(int),
252 .proc_handler
= &proc_dointvec
,
257 static ctl_table rtc_root
[] = {
268 static ctl_table dev_root
[] = {
279 static struct ctl_table_header
*sysctl_header
;
281 static int __init
init_sysctl(void)
283 sysctl_header
= register_sysctl_table(dev_root
, 0);
287 static void __exit
cleanup_sysctl(void)
289 unregister_sysctl_table(sysctl_header
);
293 * Now all the various file operations that we export.
296 static ssize_t
rtc_read(struct file
*file
, char *buf
,
297 size_t count
, loff_t
*ppos
)
302 DECLARE_WAITQUEUE(wait
, current
);
306 if (rtc_has_irq
== 0)
309 if (count
< sizeof(unsigned))
312 add_wait_queue(&rtc_wait
, &wait
);
315 /* First make it right. Then make it fast. Putting this whole
316 * block within the parentheses of a while would be too
317 * confusing. And no, xchg() is not the answer. */
319 __set_current_state(TASK_INTERRUPTIBLE
);
321 spin_lock_irq (&rtc_lock
);
324 spin_unlock_irq (&rtc_lock
);
329 if (file
->f_flags
& O_NONBLOCK
) {
333 if (signal_pending(current
)) {
334 retval
= -ERESTARTSYS
;
340 if (count
< sizeof(unsigned long))
341 retval
= put_user(data
, (unsigned int *)buf
) ?: sizeof(int);
343 retval
= put_user(data
, (unsigned long *)buf
) ?: sizeof(long);
345 current
->state
= TASK_RUNNING
;
346 remove_wait_queue(&rtc_wait
, &wait
);
352 static int rtc_do_ioctl(unsigned int cmd
, unsigned long arg
, int kernel
)
354 struct rtc_time wtime
;
357 if (rtc_has_irq
== 0) {
374 case RTC_AIE_OFF
: /* Mask alarm int. enab. bit */
376 mask_rtc_irq_bit(RTC_AIE
);
379 case RTC_AIE_ON
: /* Allow alarm interrupts. */
381 set_rtc_irq_bit(RTC_AIE
);
384 case RTC_PIE_OFF
: /* Mask periodic int. enab. bit */
386 mask_rtc_irq_bit(RTC_PIE
);
387 if (rtc_status
& RTC_TIMER_ON
) {
388 spin_lock_irq (&rtc_lock
);
389 rtc_status
&= ~RTC_TIMER_ON
;
390 del_timer(&rtc_irq_timer
);
391 spin_unlock_irq (&rtc_lock
);
395 case RTC_PIE_ON
: /* Allow periodic ints */
399 * We don't really want Joe User enabling more
400 * than 64Hz of interrupts on a multi-user machine.
402 if (!kernel
&& (rtc_freq
> rtc_max_user_freq
) &&
403 (!capable(CAP_SYS_RESOURCE
)))
406 if (!(rtc_status
& RTC_TIMER_ON
)) {
407 spin_lock_irq (&rtc_lock
);
408 rtc_irq_timer
.expires
= jiffies
+ HZ
/rtc_freq
+ 2*HZ
/100;
409 add_timer(&rtc_irq_timer
);
410 rtc_status
|= RTC_TIMER_ON
;
411 spin_unlock_irq (&rtc_lock
);
413 set_rtc_irq_bit(RTC_PIE
);
416 case RTC_UIE_OFF
: /* Mask ints from RTC updates. */
418 mask_rtc_irq_bit(RTC_UIE
);
421 case RTC_UIE_ON
: /* Allow ints for RTC updates. */
423 set_rtc_irq_bit(RTC_UIE
);
427 case RTC_ALM_READ
: /* Read the present alarm time */
430 * This returns a struct rtc_time. Reading >= 0xc0
431 * means "don't care" or "match all". Only the tm_hour,
432 * tm_min, and tm_sec values are filled in.
434 memset(&wtime
, 0, sizeof(struct rtc_time
));
435 get_rtc_alm_time(&wtime
);
438 case RTC_ALM_SET
: /* Store a time into the alarm */
441 * This expects a struct rtc_time. Writing 0xff means
442 * "don't care" or "match all". Only the tm_hour,
443 * tm_min and tm_sec are used.
445 unsigned char hrs
, min
, sec
;
446 struct rtc_time alm_tm
;
448 if (copy_from_user(&alm_tm
, (struct rtc_time
*)arg
,
449 sizeof(struct rtc_time
)))
452 hrs
= alm_tm
.tm_hour
;
456 spin_lock_irq(&rtc_lock
);
457 if (hpet_set_alarm_time(hrs
, min
, sec
)) {
459 * Fallthru and set alarm time in CMOS too,
460 * so that we will get proper value in RTC_ALM_READ
463 if (!(CMOS_READ(RTC_CONTROL
) & RTC_DM_BINARY
) ||
466 if (sec
< 60) BIN_TO_BCD(sec
);
469 if (min
< 60) BIN_TO_BCD(min
);
472 if (hrs
< 24) BIN_TO_BCD(hrs
);
475 CMOS_WRITE(hrs
, RTC_HOURS_ALARM
);
476 CMOS_WRITE(min
, RTC_MINUTES_ALARM
);
477 CMOS_WRITE(sec
, RTC_SECONDS_ALARM
);
478 spin_unlock_irq(&rtc_lock
);
482 case RTC_RD_TIME
: /* Read the time/date from RTC */
484 memset(&wtime
, 0, sizeof(struct rtc_time
));
485 rtc_get_rtc_time(&wtime
);
488 case RTC_SET_TIME
: /* Set the RTC */
490 struct rtc_time rtc_tm
;
491 unsigned char mon
, day
, hrs
, min
, sec
, leap_yr
;
492 unsigned char save_control
, save_freq_select
;
494 #ifdef CONFIG_DECSTATION
495 unsigned int real_yrs
;
498 if (!capable(CAP_SYS_TIME
))
501 if (copy_from_user(&rtc_tm
, (struct rtc_time
*)arg
,
502 sizeof(struct rtc_time
)))
505 yrs
= rtc_tm
.tm_year
+ 1900;
506 mon
= rtc_tm
.tm_mon
+ 1; /* tm_mon starts at zero */
507 day
= rtc_tm
.tm_mday
;
508 hrs
= rtc_tm
.tm_hour
;
515 leap_yr
= ((!(yrs
% 4) && (yrs
% 100)) || !(yrs
% 400));
517 if ((mon
> 12) || (day
== 0))
520 if (day
> (days_in_mo
[mon
] + ((mon
== 2) && leap_yr
)))
523 if ((hrs
>= 24) || (min
>= 60) || (sec
>= 60))
526 if ((yrs
-= epoch
) > 255) /* They are unsigned */
529 spin_lock_irq(&rtc_lock
);
530 #ifdef CONFIG_DECSTATION
535 * We want to keep the year set to 73 until March
536 * for non-leap years, so that Feb, 29th is handled
539 if (!leap_yr
&& mon
< 3) {
544 /* These limits and adjustments are independent of
545 * whether the chip is in binary mode or not.
548 spin_unlock_irq(&rtc_lock
);
554 if (!(CMOS_READ(RTC_CONTROL
) & RTC_DM_BINARY
)
564 save_control
= CMOS_READ(RTC_CONTROL
);
565 CMOS_WRITE((save_control
|RTC_SET
), RTC_CONTROL
);
566 save_freq_select
= CMOS_READ(RTC_FREQ_SELECT
);
567 CMOS_WRITE((save_freq_select
|RTC_DIV_RESET2
), RTC_FREQ_SELECT
);
569 #ifdef CONFIG_DECSTATION
570 CMOS_WRITE(real_yrs
, RTC_DEC_YEAR
);
572 CMOS_WRITE(yrs
, RTC_YEAR
);
573 CMOS_WRITE(mon
, RTC_MONTH
);
574 CMOS_WRITE(day
, RTC_DAY_OF_MONTH
);
575 CMOS_WRITE(hrs
, RTC_HOURS
);
576 CMOS_WRITE(min
, RTC_MINUTES
);
577 CMOS_WRITE(sec
, RTC_SECONDS
);
579 CMOS_WRITE(save_control
, RTC_CONTROL
);
580 CMOS_WRITE(save_freq_select
, RTC_FREQ_SELECT
);
582 spin_unlock_irq(&rtc_lock
);
586 case RTC_IRQP_READ
: /* Read the periodic IRQ rate. */
588 return put_user(rtc_freq
, (unsigned long *)arg
);
590 case RTC_IRQP_SET
: /* Set periodic IRQ rate. */
596 * The max we can do is 8192Hz.
598 if ((arg
< 2) || (arg
> 8192))
601 * We don't really want Joe User generating more
602 * than 64Hz of interrupts on a multi-user machine.
604 if (!kernel
&& (arg
> rtc_max_user_freq
) && (!capable(CAP_SYS_RESOURCE
)))
607 while (arg
> (1<<tmp
))
611 * Check that the input was really a power of 2.
616 spin_lock_irq(&rtc_lock
);
617 if (hpet_set_periodic_freq(arg
)) {
618 spin_unlock_irq(&rtc_lock
);
623 val
= CMOS_READ(RTC_FREQ_SELECT
) & 0xf0;
625 CMOS_WRITE(val
, RTC_FREQ_SELECT
);
626 spin_unlock_irq(&rtc_lock
);
630 case RTC_EPOCH_READ
: /* Read the epoch. */
632 return put_user (epoch
, (unsigned long *)arg
);
634 case RTC_EPOCH_SET
: /* Set the epoch. */
637 * There were no RTC clocks before 1900.
642 if (!capable(CAP_SYS_TIME
))
651 return copy_to_user((void *)arg
, &wtime
, sizeof wtime
) ? -EFAULT
: 0;
654 static int rtc_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
,
657 return rtc_do_ioctl(cmd
, arg
, 0);
661 * We enforce only one user at a time here with the open/close.
662 * Also clear the previous interrupt data on an open, and clean
663 * up things on a close.
666 /* We use rtc_lock to protect against concurrent opens. So the BKL is not
667 * needed here. Or anywhere else in this driver. */
668 static int rtc_open(struct inode
*inode
, struct file
*file
)
670 spin_lock_irq (&rtc_lock
);
672 if(rtc_status
& RTC_IS_OPEN
)
675 rtc_status
|= RTC_IS_OPEN
;
678 spin_unlock_irq (&rtc_lock
);
682 spin_unlock_irq (&rtc_lock
);
686 static int rtc_fasync (int fd
, struct file
*filp
, int on
)
689 return fasync_helper (fd
, filp
, on
, &rtc_async_queue
);
692 static int rtc_release(struct inode
*inode
, struct file
*file
)
697 if (rtc_has_irq
== 0)
701 * Turn off all interrupts once the device is no longer
702 * in use, and clear the data.
705 spin_lock_irq(&rtc_lock
);
706 if (!hpet_mask_rtc_irq_bit(RTC_PIE
| RTC_AIE
| RTC_UIE
)) {
707 tmp
= CMOS_READ(RTC_CONTROL
);
711 CMOS_WRITE(tmp
, RTC_CONTROL
);
712 CMOS_READ(RTC_INTR_FLAGS
);
714 if (rtc_status
& RTC_TIMER_ON
) {
715 rtc_status
&= ~RTC_TIMER_ON
;
716 del_timer(&rtc_irq_timer
);
718 spin_unlock_irq(&rtc_lock
);
720 if (file
->f_flags
& FASYNC
) {
721 rtc_fasync (-1, file
, 0);
726 spin_lock_irq (&rtc_lock
);
728 rtc_status
&= ~RTC_IS_OPEN
;
729 spin_unlock_irq (&rtc_lock
);
734 /* Called without the kernel lock - fine */
735 static unsigned int rtc_poll(struct file
*file
, poll_table
*wait
)
739 if (rtc_has_irq
== 0)
742 poll_wait(file
, &rtc_wait
, wait
);
744 spin_lock_irq (&rtc_lock
);
746 spin_unlock_irq (&rtc_lock
);
749 return POLLIN
| POLLRDNORM
;
758 EXPORT_SYMBOL(rtc_register
);
759 EXPORT_SYMBOL(rtc_unregister
);
760 EXPORT_SYMBOL(rtc_control
);
762 int rtc_register(rtc_task_t
*task
)
767 if (task
== NULL
|| task
->func
== NULL
)
769 spin_lock_irq(&rtc_lock
);
770 if (rtc_status
& RTC_IS_OPEN
) {
771 spin_unlock_irq(&rtc_lock
);
774 spin_lock(&rtc_task_lock
);
776 spin_unlock(&rtc_task_lock
);
777 spin_unlock_irq(&rtc_lock
);
780 rtc_status
|= RTC_IS_OPEN
;
782 spin_unlock(&rtc_task_lock
);
783 spin_unlock_irq(&rtc_lock
);
788 int rtc_unregister(rtc_task_t
*task
)
795 spin_lock_irq(&rtc_lock
);
796 spin_lock(&rtc_task_lock
);
797 if (rtc_callback
!= task
) {
798 spin_unlock(&rtc_task_lock
);
799 spin_unlock_irq(&rtc_lock
);
804 /* disable controls */
805 if (!hpet_mask_rtc_irq_bit(RTC_PIE
| RTC_AIE
| RTC_UIE
)) {
806 tmp
= CMOS_READ(RTC_CONTROL
);
810 CMOS_WRITE(tmp
, RTC_CONTROL
);
811 CMOS_READ(RTC_INTR_FLAGS
);
813 if (rtc_status
& RTC_TIMER_ON
) {
814 rtc_status
&= ~RTC_TIMER_ON
;
815 del_timer(&rtc_irq_timer
);
817 rtc_status
&= ~RTC_IS_OPEN
;
818 spin_unlock(&rtc_task_lock
);
819 spin_unlock_irq(&rtc_lock
);
824 int rtc_control(rtc_task_t
*task
, unsigned int cmd
, unsigned long arg
)
829 spin_lock_irq(&rtc_task_lock
);
830 if (rtc_callback
!= task
) {
831 spin_unlock_irq(&rtc_task_lock
);
834 spin_unlock_irq(&rtc_task_lock
);
835 return rtc_do_ioctl(cmd
, arg
, 1);
841 * The various file operations we support.
844 static struct file_operations rtc_fops
= {
845 .owner
= THIS_MODULE
,
853 .release
= rtc_release
,
854 .fasync
= rtc_fasync
,
857 static struct miscdevice rtc_dev
=
865 static irqreturn_t (*rtc_int_handler_ptr
)(int irq
, void *dev_id
, struct pt_regs
*regs
);
868 static int __init
rtc_init(void)
870 #if defined(__alpha__) || defined(__mips__)
871 unsigned int year
, ctrl
;
872 unsigned long uip_watchdog
;
876 struct linux_ebus
*ebus
;
877 struct linux_ebus_device
*edev
;
879 struct sparc_isa_bridge
*isa_br
;
880 struct sparc_isa_device
*isa_dev
;
885 for_each_ebus(ebus
) {
886 for_each_ebusdev(edev
, ebus
) {
887 if(strcmp(edev
->prom_name
, "rtc") == 0) {
888 rtc_port
= edev
->resource
[0].start
;
889 rtc_irq
= edev
->irqs
[0];
895 for_each_isa(isa_br
) {
896 for_each_isadev(isa_dev
, isa_br
) {
897 if (strcmp(isa_dev
->prom_name
, "rtc") == 0) {
898 rtc_port
= isa_dev
->resource
.start
;
899 rtc_irq
= isa_dev
->irq
;
905 printk(KERN_ERR
"rtc_init: no PC rtc found\n");
909 if (rtc_irq
== PCI_IRQ_NONE
) {
915 * XXX Interrupt pin #7 in Espresso is shared between RTC and
916 * PCI Slot 2 INTA# (and some INTx# in Slot 1). SA_INTERRUPT here
917 * is asking for trouble with add-on boards. Change to SA_SHIRQ.
919 if (request_irq(rtc_irq
, rtc_interrupt
, SA_INTERRUPT
, "rtc", (void *)&rtc_port
)) {
921 * Standard way for sparc to print irq's is to use
922 * __irq_itoa(). I think for EBus it's ok to use %d.
924 printk(KERN_ERR
"rtc: cannot register IRQ %d\n", rtc_irq
);
929 if (!request_region(RTC_PORT(0), RTC_IO_EXTENT
, "rtc")) {
930 printk(KERN_ERR
"rtc: I/O port %d is not free.\n", RTC_PORT (0));
935 if (is_hpet_enabled()) {
936 rtc_int_handler_ptr
= hpet_rtc_interrupt
;
938 rtc_int_handler_ptr
= rtc_interrupt
;
941 if(request_irq(RTC_IRQ
, rtc_int_handler_ptr
, SA_INTERRUPT
, "rtc", NULL
)) {
942 /* Yeah right, seeing as irq 8 doesn't even hit the bus. */
943 printk(KERN_ERR
"rtc: IRQ %d is not free.\n", RTC_IRQ
);
944 release_region(RTC_PORT(0), RTC_IO_EXTENT
);
947 hpet_rtc_timer_init();
951 #endif /* __sparc__ vs. others */
953 if (misc_register(&rtc_dev
)) {
955 free_irq(RTC_IRQ
, NULL
);
957 release_region(RTC_PORT(0), RTC_IO_EXTENT
);
960 if (create_proc_read_entry ("driver/rtc", 0, 0, rtc_read_proc
, NULL
) == NULL
) {
962 free_irq(RTC_IRQ
, NULL
);
964 release_region(RTC_PORT(0), RTC_IO_EXTENT
);
965 misc_deregister(&rtc_dev
);
969 #if defined(__alpha__) || defined(__mips__)
972 /* Each operating system on an Alpha uses its own epoch.
973 Let's try to guess which one we are using now. */
975 uip_watchdog
= jiffies
;
976 if (rtc_is_updating() != 0)
977 while (jiffies
- uip_watchdog
< 2*HZ
/100) {
982 spin_lock_irq(&rtc_lock
);
983 year
= CMOS_READ(RTC_YEAR
);
984 ctrl
= CMOS_READ(RTC_CONTROL
);
985 spin_unlock_irq(&rtc_lock
);
987 if (!(ctrl
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
)
988 BCD_TO_BIN(year
); /* This should never happen... */
992 guess
= "SRM (post-2000)";
993 } else if (year
>= 20 && year
< 48) {
995 guess
= "ARC console";
996 } else if (year
>= 48 && year
< 72) {
998 guess
= "Digital UNIX";
999 #if defined(__mips__)
1000 } else if (year
>= 72 && year
< 74) {
1002 guess
= "Digital DECstation";
1004 } else if (year
>= 70) {
1006 guess
= "Standard PC (1900)";
1010 printk(KERN_INFO
"rtc: %s epoch (%lu) detected\n", guess
, epoch
);
1013 if (rtc_has_irq
== 0)
1016 init_timer(&rtc_irq_timer
);
1017 rtc_irq_timer
.function
= rtc_dropped_irq
;
1018 spin_lock_irq(&rtc_lock
);
1020 if (!hpet_set_periodic_freq(rtc_freq
)) {
1021 /* Initialize periodic freq. to CMOS reset default, which is 1024Hz */
1022 CMOS_WRITE(((CMOS_READ(RTC_FREQ_SELECT
) & 0xF0) | 0x06), RTC_FREQ_SELECT
);
1024 spin_unlock_irq(&rtc_lock
);
1028 (void) init_sysctl();
1030 printk(KERN_INFO
"Real Time Clock Driver v" RTC_VERSION
"\n");
1035 static void __exit
rtc_exit (void)
1038 remove_proc_entry ("driver/rtc", NULL
);
1039 misc_deregister(&rtc_dev
);
1043 free_irq (rtc_irq
, &rtc_port
);
1045 release_region (RTC_PORT (0), RTC_IO_EXTENT
);
1048 free_irq (RTC_IRQ
, NULL
);
1050 #endif /* __sparc__ */
1053 module_init(rtc_init
);
1054 module_exit(rtc_exit
);
1058 * At IRQ rates >= 4096Hz, an interrupt may get lost altogether.
1059 * (usually during an IDE disk interrupt, with IRQ unmasking off)
1060 * Since the interrupt handler doesn't get called, the IRQ status
1061 * byte doesn't get read, and the RTC stops generating interrupts.
1062 * A timer is set, and will call this function if/when that happens.
1063 * To get it out of this stalled state, we just read the status.
1064 * At least a jiffy of interrupts (rtc_freq/HZ) will have been lost.
1065 * (You *really* shouldn't be trying to use a non-realtime system
1066 * for something that requires a steady > 1KHz signal anyways.)
1069 static void rtc_dropped_irq(unsigned long data
)
1073 spin_lock_irq (&rtc_lock
);
1075 if (hpet_rtc_dropped_irq()) {
1076 spin_unlock_irq(&rtc_lock
);
1080 /* Just in case someone disabled the timer from behind our back... */
1081 if (rtc_status
& RTC_TIMER_ON
)
1082 mod_timer(&rtc_irq_timer
, jiffies
+ HZ
/rtc_freq
+ 2*HZ
/100);
1084 rtc_irq_data
+= ((rtc_freq
/HZ
)<<8);
1085 rtc_irq_data
&= ~0xff;
1086 rtc_irq_data
|= (CMOS_READ(RTC_INTR_FLAGS
) & 0xF0); /* restart */
1090 spin_unlock_irq(&rtc_lock
);
1092 printk(KERN_WARNING
"rtc: lost some interrupts at %ldHz.\n", freq
);
1094 /* Now we have new data */
1095 wake_up_interruptible(&rtc_wait
);
1097 kill_fasync (&rtc_async_queue
, SIGIO
, POLL_IN
);
1102 * Info exported via "/proc/driver/rtc".
1105 static int rtc_proc_output (char *buf
)
1107 #define YN(bit) ((ctrl & bit) ? "yes" : "no")
1108 #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
);
1122 rtc_get_rtc_time(&tm
);
1125 * There is no way to tell if the luser has the RTC set for local
1126 * time or for Universal Standard Time (GMT). Probably local though.
1129 "rtc_time\t: %02d:%02d:%02d\n"
1130 "rtc_date\t: %04d-%02d-%02d\n"
1131 "rtc_epoch\t: %04lu\n",
1132 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
,
1133 tm
.tm_year
+ 1900, tm
.tm_mon
+ 1, tm
.tm_mday
, epoch
);
1135 get_rtc_alm_time(&tm
);
1138 * We implicitly assume 24hr mode here. Alarm values >= 0xc0 will
1139 * match any value for that particular field. Values that are
1140 * greater than a valid time, but less than 0xc0 shouldn't appear.
1142 p
+= sprintf(p
, "alarm\t\t: ");
1143 if (tm
.tm_hour
<= 24)
1144 p
+= sprintf(p
, "%02d:", tm
.tm_hour
);
1146 p
+= sprintf(p
, "**:");
1148 if (tm
.tm_min
<= 59)
1149 p
+= sprintf(p
, "%02d:", tm
.tm_min
);
1151 p
+= sprintf(p
, "**:");
1153 if (tm
.tm_sec
<= 59)
1154 p
+= sprintf(p
, "%02d\n", tm
.tm_sec
);
1156 p
+= sprintf(p
, "**\n");
1159 "DST_enable\t: %s\n"
1162 "square_wave\t: %s\n"
1164 "update_IRQ\t: %s\n"
1165 "periodic_IRQ\t: %s\n"
1166 "periodic_freq\t: %ld\n"
1167 "batt_status\t: %s\n",
1176 batt
? "okay" : "dead");
1183 static int rtc_read_proc(char *page
, char **start
, off_t off
,
1184 int count
, int *eof
, void *data
)
1186 int len
= rtc_proc_output (page
);
1187 if (len
<= off
+count
) *eof
= 1;
1188 *start
= page
+ off
;
1190 if (len
>count
) len
= count
;
1196 * Returns true if a clock update is in progress
1198 /* FIXME shouldn't this be above rtc_init to make it fully inlined? */
1199 static inline unsigned char rtc_is_updating(void)
1203 spin_lock_irq(&rtc_lock
);
1204 uip
= (CMOS_READ(RTC_FREQ_SELECT
) & RTC_UIP
);
1205 spin_unlock_irq(&rtc_lock
);
1209 void rtc_get_rtc_time(struct rtc_time
*rtc_tm
)
1211 unsigned long uip_watchdog
= jiffies
;
1213 #ifdef CONFIG_DECSTATION
1214 unsigned int real_year
;
1218 * read RTC once any update in progress is done. The update
1219 * can take just over 2ms. We wait 10 to 20ms. There is no need to
1220 * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
1221 * If you need to know *exactly* when a second has started, enable
1222 * periodic update complete interrupts, (via ioctl) and then
1223 * immediately read /dev/rtc which will block until you get the IRQ.
1224 * Once the read clears, read the RTC time (again via ioctl). Easy.
1227 if (rtc_is_updating() != 0)
1228 while (jiffies
- uip_watchdog
< 2*HZ
/100) {
1234 * Only the values that we read from the RTC are set. We leave
1235 * tm_wday, tm_yday and tm_isdst untouched. Even though the
1236 * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
1237 * by the RTC when initially set to a non-zero value.
1239 spin_lock_irq(&rtc_lock
);
1240 rtc_tm
->tm_sec
= CMOS_READ(RTC_SECONDS
);
1241 rtc_tm
->tm_min
= CMOS_READ(RTC_MINUTES
);
1242 rtc_tm
->tm_hour
= CMOS_READ(RTC_HOURS
);
1243 rtc_tm
->tm_mday
= CMOS_READ(RTC_DAY_OF_MONTH
);
1244 rtc_tm
->tm_mon
= CMOS_READ(RTC_MONTH
);
1245 rtc_tm
->tm_year
= CMOS_READ(RTC_YEAR
);
1246 #ifdef CONFIG_DECSTATION
1247 real_year
= CMOS_READ(RTC_DEC_YEAR
);
1249 ctrl
= CMOS_READ(RTC_CONTROL
);
1250 spin_unlock_irq(&rtc_lock
);
1252 if (!(ctrl
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
)
1254 BCD_TO_BIN(rtc_tm
->tm_sec
);
1255 BCD_TO_BIN(rtc_tm
->tm_min
);
1256 BCD_TO_BIN(rtc_tm
->tm_hour
);
1257 BCD_TO_BIN(rtc_tm
->tm_mday
);
1258 BCD_TO_BIN(rtc_tm
->tm_mon
);
1259 BCD_TO_BIN(rtc_tm
->tm_year
);
1262 #ifdef CONFIG_DECSTATION
1263 rtc_tm
->tm_year
+= real_year
- 72;
1267 * Account for differences between how the RTC uses the values
1268 * and how they are defined in a struct rtc_time;
1270 if ((rtc_tm
->tm_year
+= (epoch
- 1900)) <= 69)
1271 rtc_tm
->tm_year
+= 100;
1276 static void get_rtc_alm_time(struct rtc_time
*alm_tm
)
1281 * Only the values that we read from the RTC are set. That
1282 * means only tm_hour, tm_min, and tm_sec.
1284 spin_lock_irq(&rtc_lock
);
1285 alm_tm
->tm_sec
= CMOS_READ(RTC_SECONDS_ALARM
);
1286 alm_tm
->tm_min
= CMOS_READ(RTC_MINUTES_ALARM
);
1287 alm_tm
->tm_hour
= CMOS_READ(RTC_HOURS_ALARM
);
1288 ctrl
= CMOS_READ(RTC_CONTROL
);
1289 spin_unlock_irq(&rtc_lock
);
1291 if (!(ctrl
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
)
1293 BCD_TO_BIN(alm_tm
->tm_sec
);
1294 BCD_TO_BIN(alm_tm
->tm_min
);
1295 BCD_TO_BIN(alm_tm
->tm_hour
);
1301 * Used to disable/enable interrupts for any one of UIE, AIE, PIE.
1302 * Rumour has it that if you frob the interrupt enable/disable
1303 * bits in RTC_CONTROL, you should read RTC_INTR_FLAGS, to
1304 * ensure you actually start getting interrupts. Probably for
1305 * compatibility with older/broken chipset RTC implementations.
1306 * We also clear out any old irq data after an ioctl() that
1307 * meddles with the interrupt enable/disable bits.
1310 static void mask_rtc_irq_bit(unsigned char bit
)
1314 spin_lock_irq(&rtc_lock
);
1315 if (hpet_mask_rtc_irq_bit(bit
)) {
1316 spin_unlock_irq(&rtc_lock
);
1319 val
= CMOS_READ(RTC_CONTROL
);
1321 CMOS_WRITE(val
, RTC_CONTROL
);
1322 CMOS_READ(RTC_INTR_FLAGS
);
1325 spin_unlock_irq(&rtc_lock
);
1328 static void set_rtc_irq_bit(unsigned char bit
)
1332 spin_lock_irq(&rtc_lock
);
1333 if (hpet_set_rtc_irq_bit(bit
)) {
1334 spin_unlock_irq(&rtc_lock
);
1337 val
= CMOS_READ(RTC_CONTROL
);
1339 CMOS_WRITE(val
, RTC_CONTROL
);
1340 CMOS_READ(RTC_INTR_FLAGS
);
1343 spin_unlock_irq(&rtc_lock
);
1347 MODULE_AUTHOR("Paul Gortmaker");
1348 MODULE_LICENSE("GPL");
1349 MODULE_ALIAS_MISCDEV(RTC_MINOR
);