54 #define RTC_VERSION "1.12b"
65 #include <linux/module.h>
66 #include <linux/kernel.h>
67 #include <linux/types.h>
70 #include <linux/fcntl.h>
73 #include <linux/poll.h>
77 #include <linux/sched.h>
78 #include <linux/sysctl.h>
79 #include <linux/wait.h>
85 #include <asm/current.h>
96 static unsigned long rtc_port;
100 #ifdef CONFIG_HPET_EMULATE_RTC
105 static int rtc_has_irq = 1;
108 #ifndef CONFIG_HPET_EMULATE_RTC
109 #define is_hpet_enabled() 0
110 #define hpet_set_alarm_time(hrs, min, sec) 0
111 #define hpet_set_periodic_freq(arg) 0
112 #define hpet_mask_rtc_irq_bit(arg) 0
113 #define hpet_set_rtc_irq_bit(arg) 0
114 #define hpet_rtc_timer_init() do { } while (0)
115 #define hpet_rtc_dropped_irq() 0
116 #define hpet_register_irq_handler(h) ({ 0; })
117 #define hpet_unregister_irq_handler(h) ({ 0; })
138 static void rtc_dropped_irq(
unsigned long data);
140 static DEFINE_TIMER(rtc_irq_timer, rtc_dropped_irq, 0, 0);
144 size_t count, loff_t *ppos);
146 static long rtc_ioctl(
struct file *
file,
unsigned int cmd,
unsigned long arg);
147 static void rtc_get_rtc_time(
struct rtc_time *rtc_tm);
153 static void get_rtc_alm_time(
struct rtc_time *alm_tm);
155 static void set_rtc_irq_bit_locked(
unsigned char bit);
156 static void mask_rtc_irq_bit_locked(
unsigned char bit);
158 static inline void set_rtc_irq_bit(
unsigned char bit)
160 spin_lock_irq(&rtc_lock);
161 set_rtc_irq_bit_locked(bit);
162 spin_unlock_irq(&rtc_lock);
165 static void mask_rtc_irq_bit(
unsigned char bit)
167 spin_lock_irq(&rtc_lock);
168 mask_rtc_irq_bit_locked(bit);
169 spin_unlock_irq(&rtc_lock);
173 #ifdef CONFIG_PROC_FS
181 #define RTC_IS_OPEN 0x01
182 #define RTC_TIMER_ON 0x02
192 static unsigned long rtc_freq;
193 static unsigned long rtc_irq_data;
194 static unsigned long rtc_max_user_freq = 64;
209 static unsigned long epoch = 1900;
211 static const unsigned char days_in_mo[] =
212 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
217 static inline unsigned char rtc_is_updating(
void)
224 spin_unlock_irqrestore(&rtc_lock, flags);
248 spin_lock(&rtc_lock);
249 rtc_irq_data += 0x100;
250 rtc_irq_data &= ~0xff;
257 rtc_irq_data |= (
unsigned long)irq & 0xF0;
265 spin_unlock(&rtc_lock);
268 spin_lock(&rtc_task_lock);
271 spin_unlock(&rtc_task_lock);
286 .data = &rtc_max_user_freq,
287 .maxlen =
sizeof(
int),
314 static int __init init_sysctl(
void)
320 static void __exit cleanup_sysctl(
void)
330 size_t count, loff_t *ppos)
339 if (rtc_has_irq == 0)
350 if (count !=
sizeof(
unsigned int) && count !=
sizeof(
unsigned long))
362 spin_lock_irq(&rtc_lock);
365 spin_unlock_irq(&rtc_lock);
381 if (count ==
sizeof(
unsigned int)) {
383 (
unsigned int __user *)buf) ?:
sizeof(
int);
386 (
unsigned long __user *)buf) ?:
sizeof(
long);
398 static int rtc_do_ioctl(
unsigned int cmd,
unsigned long arg,
int kernel)
403 if (rtc_has_irq == 0) {
436 mask_rtc_irq_bit_locked(
RTC_PIE);
441 spin_unlock_irqrestore(&rtc_lock, flags);
454 if (!kernel && (rtc_freq > rtc_max_user_freq) &&
464 set_rtc_irq_bit_locked(
RTC_PIE);
465 spin_unlock_irqrestore(&rtc_lock, flags);
488 get_rtc_alm_time(&wtime);
498 unsigned char hrs,
min,
sec;
505 hrs = alm_tm.tm_hour;
509 spin_lock_irq(&rtc_lock);
536 spin_unlock_irq(&rtc_lock);
543 rtc_get_rtc_time(&wtime);
549 unsigned char mon,
day, hrs,
min,
sec, leap_yr;
550 unsigned char save_control, save_freq_select;
552 #ifdef CONFIG_MACH_DECSTATION
553 unsigned int real_yrs;
563 yrs = rtc_tm.tm_year + 1900;
564 mon = rtc_tm.tm_mon + 1;
565 day = rtc_tm.tm_mday;
566 hrs = rtc_tm.tm_hour;
573 leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
575 if ((mon > 12) || (day == 0))
578 if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
581 if ((hrs >= 24) || (min >= 60) || (sec >= 60))
588 spin_lock_irq(&rtc_lock);
589 #ifdef CONFIG_MACH_DECSTATION
598 if (!leap_yr && mon < 3) {
607 spin_unlock_irq(&rtc_lock);
628 #ifdef CONFIG_MACH_DECSTATION
641 spin_unlock_irq(&rtc_lock);
647 return put_user(rtc_freq, (
unsigned long __user *)arg);
659 if ((arg < 2) || (arg > 8192))
665 if (!kernel && (arg > rtc_max_user_freq) &&
669 while (arg > (1<<tmp))
682 spin_unlock_irqrestore(&rtc_lock, flags);
689 spin_unlock_irqrestore(&rtc_lock, flags);
715 &wtime,
sizeof wtime) ? -
EFAULT : 0;
718 static long rtc_ioctl(
struct file *file,
unsigned int cmd,
unsigned long arg)
721 ret = rtc_do_ioctl(cmd, arg, 0);
730 static int rtc_open(
struct inode *
inode,
struct file *file)
732 spin_lock_irq(&rtc_lock);
740 spin_unlock_irq(&rtc_lock);
744 spin_unlock_irq(&rtc_lock);
748 static int rtc_fasync(
int fd,
struct file *filp,
int on)
753 static int rtc_release(
struct inode *inode,
struct file *file)
758 if (rtc_has_irq == 0)
766 spin_lock_irq(&rtc_lock);
779 spin_unlock_irq(&rtc_lock);
784 spin_lock_irq(&rtc_lock);
787 spin_unlock_irq(&rtc_lock);
797 if (rtc_has_irq == 0)
800 poll_wait(file, &rtc_wait, wait);
802 spin_lock_irq(&rtc_lock);
804 spin_unlock_irq(&rtc_lock);
824 spin_lock(&rtc_task_lock);
826 spin_unlock(&rtc_task_lock);
832 spin_unlock(&rtc_task_lock);
847 spin_lock(&rtc_task_lock);
848 if (rtc_callback != task) {
849 spin_unlock(&rtc_task_lock);
869 spin_unlock(&rtc_task_lock);
885 if (rtc_callback != task) {
886 spin_unlock_irqrestore(&rtc_task_lock, flags);
889 spin_unlock_irqrestore(&rtc_task_lock, flags);
890 return rtc_do_ioctl(cmd, arg, 1);
906 .unlocked_ioctl = rtc_ioctl,
908 .release = rtc_release,
909 .fasync = rtc_fasync,
918 #ifdef CONFIG_PROC_FS
921 .open = rtc_proc_open,
945 static void rtc_release_region(
void)
953 static int __init rtc_init(
void)
955 #ifdef CONFIG_PROC_FS
958 #if defined(__alpha__) || defined(__mips__)
962 #ifdef CONFIG_SPARC32
972 #ifdef CONFIG_SPARC32
973 for_each_node_by_name(ebus_dp,
"ebus") {
975 for (dp = ebus_dp;
dp; dp = dp->
sibling) {
980 rtc_irq = op->irqs[0];
1001 (
void *)&rtc_port)) {
1040 rtc_int_handler_ptr = rtc_interrupt;
1048 rtc_release_region();
1064 rtc_release_region();
1068 #ifdef CONFIG_PROC_FS
1069 ent = proc_create(
"driver/rtc", 0,
NULL, &rtc_proc_fops);
1074 #if defined(__alpha__) || defined(__mips__)
1080 if (rtc_is_updating() != 0)
1083 spin_lock_irq(&rtc_lock);
1086 spin_unlock_irq(&rtc_lock);
1093 guess =
"SRM (post-2000)";
1094 }
else if (year >= 20 && year < 48) {
1096 guess =
"ARC console";
1097 }
else if (year >= 48 && year < 72) {
1099 guess =
"Digital UNIX";
1100 #if defined(__mips__)
1101 }
else if (year >= 72 && year < 74) {
1103 guess =
"Digital DECstation";
1105 }
else if (year >= 70) {
1107 guess =
"Standard PC (1900)";
1115 if (rtc_has_irq == 0)
1118 spin_lock_irq(&rtc_lock);
1128 spin_unlock_irq(&rtc_lock);
1132 (
void) init_sysctl();
1139 static void __exit rtc_exit(
void)
1145 #ifdef CONFIG_SPARC32
1149 rtc_release_region();
1175 static void rtc_dropped_irq(
unsigned long data)
1179 spin_lock_irq(&rtc_lock);
1182 spin_unlock_irq(&rtc_lock);
1188 mod_timer(&rtc_irq_timer, jiffies +
HZ/rtc_freq + 2*
HZ/100);
1190 rtc_irq_data += ((rtc_freq/
HZ)<<8);
1191 rtc_irq_data &= ~0xff;
1196 spin_unlock_irq(&rtc_lock);
1208 #ifdef CONFIG_PROC_FS
1213 static int rtc_proc_show(
struct seq_file *seq,
void *
v)
1215 #define YN(bit) ((ctrl & bit) ? "yes" : "no")
1216 #define NY(bit) ((ctrl & bit) ? "no" : "yes")
1218 unsigned char batt,
ctrl;
1221 spin_lock_irq(&rtc_lock);
1225 spin_unlock_irq(&rtc_lock);
1228 rtc_get_rtc_time(&
tm);
1235 "rtc_time\t: %02d:%02d:%02d\n"
1236 "rtc_date\t: %04d-%02d-%02d\n"
1237 "rtc_epoch\t: %04lu\n",
1241 get_rtc_alm_time(&
tm);
1265 "DST_enable\t: %s\n"
1268 "square_wave\t: %s\n"
1270 "update_IRQ\t: %s\n"
1271 "periodic_IRQ\t: %s\n"
1272 "periodic_freq\t: %ld\n"
1273 "batt_status\t: %s\n",
1282 batt ?
"okay" :
"dead");
1289 static int rtc_proc_open(
struct inode *inode,
struct file *file)
1295 static void rtc_get_rtc_time(
struct rtc_time *rtc_tm)
1299 #ifdef CONFIG_MACH_DECSTATION
1300 unsigned int real_year;
1313 while (rtc_is_updating() != 0 &&
1333 #ifdef CONFIG_MACH_DECSTATION
1337 spin_unlock_irqrestore(&rtc_lock, flags);
1349 #ifdef CONFIG_MACH_DECSTATION
1350 rtc_tm->
tm_year += real_year - 72;
1364 static void get_rtc_alm_time(
struct rtc_time *alm_tm)
1372 spin_lock_irq(&rtc_lock);
1377 spin_unlock_irq(&rtc_lock);
1397 static void mask_rtc_irq_bit_locked(
unsigned char bit)
1411 static void set_rtc_irq_bit_locked(
unsigned char bit)