linux 2.6.16.38 w/ vs2.0.3-rc1
[linux-2.6.git] / drivers / char / rtc.c
1 /*
2  *      Real Time Clock interface for Linux     
3  *
4  *      Copyright (C) 1996 Paul Gortmaker
5  *
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.
10  *
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.
15  *
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.
22  *
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.
27  *
28  *      Based on other minimal char device drivers, like Alan's
29  *      watchdog, Ted's random, etc. etc.
30  *
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
49  *      1.12ac  Alan Cox: Allow read access to the day of week register
50  */
51
52 #define RTC_VERSION             "1.12ac"
53
54 #define RTC_IO_EXTENT   0x8
55
56 /*
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.
61  *      this driver.)
62  */
63
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/seq_file.h>
77 #include <linux/spinlock.h>
78 #include <linux/sysctl.h>
79 #include <linux/wait.h>
80 #include <linux/bcd.h>
81 #include <linux/delay.h>
82
83 #include <asm/current.h>
84 #include <asm/uaccess.h>
85 #include <asm/system.h>
86
87 #if defined(__i386__)
88 #include <asm/hpet.h>
89 #endif
90
91 #ifdef __sparc__
92 #include <linux/pci.h>
93 #include <asm/ebus.h>
94 #ifdef __sparc_v9__
95 #include <asm/isa.h>
96 #endif
97
98 static unsigned long rtc_port;
99 static int rtc_irq = PCI_IRQ_NONE;
100 #endif
101
102 #ifdef  CONFIG_HPET_RTC_IRQ
103 #undef  RTC_IRQ
104 #endif
105
106 #ifdef RTC_IRQ
107 static int rtc_has_irq = 1;
108 #endif
109
110 #ifndef CONFIG_HPET_EMULATE_RTC
111 #define is_hpet_enabled()                       0
112 #define hpet_set_alarm_time(hrs, min, sec)      0
113 #define hpet_set_periodic_freq(arg)             0
114 #define hpet_mask_rtc_irq_bit(arg)              0
115 #define hpet_set_rtc_irq_bit(arg)               0
116 #define hpet_rtc_timer_init()                   do { } while (0)
117 #define hpet_rtc_dropped_irq()                  0
118 static inline irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs) {return 0;}
119 #else
120 extern irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs);
121 #endif
122
123 /*
124  *      We sponge a minor off of the misc major. No need slurping
125  *      up another valuable major dev number for this. If you add
126  *      an ioctl, make sure you don't conflict with SPARC's RTC
127  *      ioctls.
128  */
129
130 static struct fasync_struct *rtc_async_queue;
131
132 static DECLARE_WAIT_QUEUE_HEAD(rtc_wait);
133
134 #ifdef RTC_IRQ
135 static struct timer_list rtc_irq_timer;
136 #endif
137
138 static ssize_t rtc_read(struct file *file, char __user *buf,
139                         size_t count, loff_t *ppos);
140
141 static int rtc_ioctl(struct inode *inode, struct file *file,
142                      unsigned int cmd, unsigned long arg);
143
144 #ifdef RTC_IRQ
145 static unsigned int rtc_poll(struct file *file, poll_table *wait);
146 #endif
147
148 static void get_rtc_alm_time (struct rtc_time *alm_tm);
149 #ifdef RTC_IRQ
150 static void rtc_dropped_irq(unsigned long data);
151
152 static void set_rtc_irq_bit_locked(unsigned char bit);
153 static void mask_rtc_irq_bit_locked(unsigned char bit);
154
155 static inline void set_rtc_irq_bit(unsigned char bit)
156 {
157         spin_lock_irq(&rtc_lock);
158         set_rtc_irq_bit_locked(bit);
159         spin_unlock_irq(&rtc_lock);
160 }
161
162 static void mask_rtc_irq_bit(unsigned char bit)
163 {
164         spin_lock_irq(&rtc_lock);
165         mask_rtc_irq_bit_locked(bit);
166         spin_unlock_irq(&rtc_lock);
167 }
168 #endif
169
170 static int rtc_proc_open(struct inode *inode, struct file *file);
171
172 /*
173  *      Bits in rtc_status. (6 bits of room for future expansion)
174  */
175
176 #define RTC_IS_OPEN             0x01    /* means /dev/rtc is in use     */
177 #define RTC_TIMER_ON            0x02    /* missed irq timer active      */
178
179 /*
180  * rtc_status is never changed by rtc_interrupt, and ioctl/open/close is
181  * protected by the big kernel lock. However, ioctl can still disable the timer
182  * in rtc_status and then with del_timer after the interrupt has read
183  * rtc_status but before mod_timer is called, which would then reenable the
184  * timer (but you would need to have an awful timing before you'd trip on it)
185  */
186 static unsigned long rtc_status = 0;    /* bitmapped status byte.       */
187 static unsigned long rtc_freq = 0;      /* Current periodic IRQ rate    */
188 static unsigned long rtc_irq_data = 0;  /* our output to the world      */
189 static unsigned long rtc_max_user_freq = 64; /* > this, need CAP_SYS_RESOURCE */
190
191 #ifdef RTC_IRQ
192 /*
193  * rtc_task_lock nests inside rtc_lock.
194  */
195 static DEFINE_SPINLOCK(rtc_task_lock);
196 static rtc_task_t *rtc_callback = NULL;
197 #endif
198
199 /*
200  *      If this driver ever becomes modularised, it will be really nice
201  *      to make the epoch retain its value across module reload...
202  */
203
204 static unsigned long epoch = 1900;      /* year corresponding to 0x00   */
205
206 static const unsigned char days_in_mo[] = 
207 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
208
209 /*
210  * Returns true if a clock update is in progress
211  */
212 static inline unsigned char rtc_is_updating(void)
213 {
214         unsigned long flags;
215         unsigned char uip;
216
217         spin_lock_irqsave(&rtc_lock, flags);
218         uip = (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP);
219         spin_unlock_irqrestore(&rtc_lock, flags);
220         return uip;
221 }
222
223 #ifdef RTC_IRQ
224 /*
225  *      A very tiny interrupt handler. It runs with SA_INTERRUPT set,
226  *      but there is possibility of conflicting with the set_rtc_mmss()
227  *      call (the rtc irq and the timer irq can easily run at the same
228  *      time in two different CPUs). So we need to serialize
229  *      accesses to the chip with the rtc_lock spinlock that each
230  *      architecture should implement in the timer code.
231  *      (See ./arch/XXXX/kernel/time.c for the set_rtc_mmss() function.)
232  */
233
234 irqreturn_t rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
235 {
236         /*
237          *      Can be an alarm interrupt, update complete interrupt,
238          *      or a periodic interrupt. We store the status in the
239          *      low byte and the number of interrupts received since
240          *      the last read in the remainder of rtc_irq_data.
241          */
242
243         spin_lock (&rtc_lock);
244         rtc_irq_data += 0x100;
245         rtc_irq_data &= ~0xff;
246         if (is_hpet_enabled()) {
247                 /*
248                  * In this case it is HPET RTC interrupt handler
249                  * calling us, with the interrupt information
250                  * passed as arg1, instead of irq.
251                  */
252                 rtc_irq_data |= (unsigned long)irq & 0xF0;
253         } else {
254                 rtc_irq_data |= (CMOS_READ(RTC_INTR_FLAGS) & 0xF0);
255         }
256
257         if (rtc_status & RTC_TIMER_ON)
258                 mod_timer(&rtc_irq_timer, jiffies + HZ/rtc_freq + 2*HZ/100);
259
260         spin_unlock (&rtc_lock);
261
262         /* Now do the rest of the actions */
263         spin_lock(&rtc_task_lock);
264         if (rtc_callback)
265                 rtc_callback->func(rtc_callback->private_data);
266         spin_unlock(&rtc_task_lock);
267         wake_up_interruptible(&rtc_wait);       
268
269         kill_fasync (&rtc_async_queue, SIGIO, POLL_IN);
270
271         return IRQ_HANDLED;
272 }
273 #endif
274
275 /*
276  * sysctl-tuning infrastructure.
277  */
278 static ctl_table rtc_table[] = {
279         {
280                 .ctl_name       = 1,
281                 .procname       = "max-user-freq",
282                 .data           = &rtc_max_user_freq,
283                 .maxlen         = sizeof(int),
284                 .mode           = 0644,
285                 .proc_handler   = &proc_dointvec,
286         },
287         { .ctl_name = 0 }
288 };
289
290 static ctl_table rtc_root[] = {
291         {
292                 .ctl_name       = 1,
293                 .procname       = "rtc",
294                 .maxlen         = 0,
295                 .mode           = 0555,
296                 .child          = rtc_table,
297         },
298         { .ctl_name = 0 }
299 };
300
301 static ctl_table dev_root[] = {
302         {
303                 .ctl_name       = CTL_DEV,
304                 .procname       = "dev",
305                 .maxlen         = 0,
306                 .mode           = 0555,
307                 .child          = rtc_root,
308         },
309         { .ctl_name = 0 }
310 };
311
312 static struct ctl_table_header *sysctl_header;
313
314 static int __init init_sysctl(void)
315 {
316     sysctl_header = register_sysctl_table(dev_root, 0);
317     return 0;
318 }
319
320 static void __exit cleanup_sysctl(void)
321 {
322     unregister_sysctl_table(sysctl_header);
323 }
324
325 /*
326  *      Now all the various file operations that we export.
327  */
328
329 static ssize_t rtc_read(struct file *file, char __user *buf,
330                         size_t count, loff_t *ppos)
331 {
332 #ifndef RTC_IRQ
333         return -EIO;
334 #else
335         DECLARE_WAITQUEUE(wait, current);
336         unsigned long data;
337         ssize_t retval;
338         
339         if (rtc_has_irq == 0)
340                 return -EIO;
341
342         if (count < sizeof(unsigned))
343                 return -EINVAL;
344
345         add_wait_queue(&rtc_wait, &wait);
346
347         do {
348                 /* First make it right. Then make it fast. Putting this whole
349                  * block within the parentheses of a while would be too
350                  * confusing. And no, xchg() is not the answer. */
351
352                 __set_current_state(TASK_INTERRUPTIBLE);
353                 
354                 spin_lock_irq (&rtc_lock);
355                 data = rtc_irq_data;
356                 rtc_irq_data = 0;
357                 spin_unlock_irq (&rtc_lock);
358
359                 if (data != 0)
360                         break;
361
362                 if (file->f_flags & O_NONBLOCK) {
363                         retval = -EAGAIN;
364                         goto out;
365                 }
366                 if (signal_pending(current)) {
367                         retval = -ERESTARTSYS;
368                         goto out;
369                 }
370                 schedule();
371         } while (1);
372
373         if (count < sizeof(unsigned long))
374                 retval = put_user(data, (unsigned int __user *)buf) ?: sizeof(int); 
375         else
376                 retval = put_user(data, (unsigned long __user *)buf) ?: sizeof(long);
377  out:
378         current->state = TASK_RUNNING;
379         remove_wait_queue(&rtc_wait, &wait);
380
381         return retval;
382 #endif
383 }
384
385 static int rtc_do_ioctl(unsigned int cmd, unsigned long arg, int kernel)
386 {
387         struct rtc_time wtime; 
388
389 #ifdef RTC_IRQ
390         if (rtc_has_irq == 0) {
391                 switch (cmd) {
392                 case RTC_AIE_OFF:
393                 case RTC_AIE_ON:
394                 case RTC_PIE_OFF:
395                 case RTC_PIE_ON:
396                 case RTC_UIE_OFF:
397                 case RTC_UIE_ON:
398                 case RTC_IRQP_READ:
399                 case RTC_IRQP_SET:
400                         return -EINVAL;
401                 };
402         }
403 #endif
404
405         switch (cmd) {
406 #ifdef RTC_IRQ
407         case RTC_AIE_OFF:       /* Mask alarm int. enab. bit    */
408         {
409                 mask_rtc_irq_bit(RTC_AIE);
410                 return 0;
411         }
412         case RTC_AIE_ON:        /* Allow alarm interrupts.      */
413         {
414                 set_rtc_irq_bit(RTC_AIE);
415                 return 0;
416         }
417         case RTC_PIE_OFF:       /* Mask periodic int. enab. bit */
418         {
419                 unsigned long flags; /* can be called from isr via rtc_control() */
420                 spin_lock_irqsave (&rtc_lock, flags);
421                 mask_rtc_irq_bit_locked(RTC_PIE);
422                 if (rtc_status & RTC_TIMER_ON) {
423                         rtc_status &= ~RTC_TIMER_ON;
424                         del_timer(&rtc_irq_timer);
425                 }
426                 spin_unlock_irqrestore (&rtc_lock, flags);
427                 return 0;
428         }
429         case RTC_PIE_ON:        /* Allow periodic ints          */
430         {
431                 unsigned long flags; /* can be called from isr via rtc_control() */
432                 /*
433                  * We don't really want Joe User enabling more
434                  * than 64Hz of interrupts on a multi-user machine.
435                  */
436                 if (!kernel && (rtc_freq > rtc_max_user_freq) &&
437                         (!capable(CAP_SYS_RESOURCE)))
438                         return -EACCES;
439
440                 spin_lock_irqsave (&rtc_lock, flags);
441                 if (!(rtc_status & RTC_TIMER_ON)) {
442                         rtc_irq_timer.expires = jiffies + HZ/rtc_freq + 2*HZ/100;
443                         add_timer(&rtc_irq_timer);
444                         rtc_status |= RTC_TIMER_ON;
445                 }
446                 set_rtc_irq_bit_locked(RTC_PIE);
447                 spin_unlock_irqrestore (&rtc_lock, flags);
448                 return 0;
449         }
450         case RTC_UIE_OFF:       /* Mask ints from RTC updates.  */
451         {
452                 mask_rtc_irq_bit(RTC_UIE);
453                 return 0;
454         }
455         case RTC_UIE_ON:        /* Allow ints for RTC updates.  */
456         {
457                 set_rtc_irq_bit(RTC_UIE);
458                 return 0;
459         }
460 #endif
461         case RTC_ALM_READ:      /* Read the present alarm time */
462         {
463                 /*
464                  * This returns a struct rtc_time. Reading >= 0xc0
465                  * means "don't care" or "match all". Only the tm_hour,
466                  * tm_min, and tm_sec values are filled in.
467                  */
468                 memset(&wtime, 0, sizeof(struct rtc_time));
469                 get_rtc_alm_time(&wtime);
470                 break; 
471         }
472         case RTC_ALM_SET:       /* Store a time into the alarm */
473         {
474                 /*
475                  * This expects a struct rtc_time. Writing 0xff means
476                  * "don't care" or "match all". Only the tm_hour,
477                  * tm_min and tm_sec are used.
478                  */
479                 unsigned char hrs, min, sec;
480                 struct rtc_time alm_tm;
481
482                 if (copy_from_user(&alm_tm, (struct rtc_time __user *)arg,
483                                    sizeof(struct rtc_time)))
484                         return -EFAULT;
485
486                 hrs = alm_tm.tm_hour;
487                 min = alm_tm.tm_min;
488                 sec = alm_tm.tm_sec;
489
490                 spin_lock_irq(&rtc_lock);
491                 if (hpet_set_alarm_time(hrs, min, sec)) {
492                         /*
493                          * Fallthru and set alarm time in CMOS too,
494                          * so that we will get proper value in RTC_ALM_READ
495                          */
496                 }
497                 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) ||
498                     RTC_ALWAYS_BCD)
499                 {
500                         if (sec < 60) BIN_TO_BCD(sec);
501                         else sec = 0xff;
502
503                         if (min < 60) BIN_TO_BCD(min);
504                         else min = 0xff;
505
506                         if (hrs < 24) BIN_TO_BCD(hrs);
507                         else hrs = 0xff;
508                 }
509                 CMOS_WRITE(hrs, RTC_HOURS_ALARM);
510                 CMOS_WRITE(min, RTC_MINUTES_ALARM);
511                 CMOS_WRITE(sec, RTC_SECONDS_ALARM);
512                 spin_unlock_irq(&rtc_lock);
513
514                 return 0;
515         }
516         case RTC_RD_TIME:       /* Read the time/date from RTC  */
517         {
518                 memset(&wtime, 0, sizeof(struct rtc_time));
519                 rtc_get_rtc_time(&wtime);
520                 break;
521         }
522         case RTC_SET_TIME:      /* Set the RTC */
523         {
524                 struct rtc_time rtc_tm;
525                 unsigned char mon, day, hrs, min, sec, leap_yr;
526                 unsigned char save_control, save_freq_select;
527                 unsigned int yrs;
528 #ifdef CONFIG_MACH_DECSTATION
529                 unsigned int real_yrs;
530 #endif
531
532                 if (!capable(CAP_SYS_TIME))
533                         return -EACCES;
534
535                 if (copy_from_user(&rtc_tm, (struct rtc_time __user *)arg,
536                                    sizeof(struct rtc_time)))
537                         return -EFAULT;
538
539                 yrs = rtc_tm.tm_year + 1900;
540                 mon = rtc_tm.tm_mon + 1;   /* tm_mon starts at zero */
541                 day = rtc_tm.tm_mday;
542                 hrs = rtc_tm.tm_hour;
543                 min = rtc_tm.tm_min;
544                 sec = rtc_tm.tm_sec;
545
546                 if (yrs < 1970)
547                         return -EINVAL;
548
549                 leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
550
551                 if ((mon > 12) || (day == 0))
552                         return -EINVAL;
553
554                 if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
555                         return -EINVAL;
556                         
557                 if ((hrs >= 24) || (min >= 60) || (sec >= 60))
558                         return -EINVAL;
559
560                 if ((yrs -= epoch) > 255)    /* They are unsigned */
561                         return -EINVAL;
562
563                 spin_lock_irq(&rtc_lock);
564 #ifdef CONFIG_MACH_DECSTATION
565                 real_yrs = yrs;
566                 yrs = 72;
567
568                 /*
569                  * We want to keep the year set to 73 until March
570                  * for non-leap years, so that Feb, 29th is handled
571                  * correctly.
572                  */
573                 if (!leap_yr && mon < 3) {
574                         real_yrs--;
575                         yrs = 73;
576                 }
577 #endif
578                 /* These limits and adjustments are independent of
579                  * whether the chip is in binary mode or not.
580                  */
581                 if (yrs > 169) {
582                         spin_unlock_irq(&rtc_lock);
583                         return -EINVAL;
584                 }
585                 if (yrs >= 100)
586                         yrs -= 100;
587
588                 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY)
589                     || RTC_ALWAYS_BCD) {
590                         BIN_TO_BCD(sec);
591                         BIN_TO_BCD(min);
592                         BIN_TO_BCD(hrs);
593                         BIN_TO_BCD(day);
594                         BIN_TO_BCD(mon);
595                         BIN_TO_BCD(yrs);
596                 }
597
598                 save_control = CMOS_READ(RTC_CONTROL);
599                 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
600                 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
601                 CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
602
603 #ifdef CONFIG_MACH_DECSTATION
604                 CMOS_WRITE(real_yrs, RTC_DEC_YEAR);
605 #endif
606                 CMOS_WRITE(yrs, RTC_YEAR);
607                 CMOS_WRITE(mon, RTC_MONTH);
608                 CMOS_WRITE(day, RTC_DAY_OF_MONTH);
609                 CMOS_WRITE(hrs, RTC_HOURS);
610                 CMOS_WRITE(min, RTC_MINUTES);
611                 CMOS_WRITE(sec, RTC_SECONDS);
612
613                 CMOS_WRITE(save_control, RTC_CONTROL);
614                 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
615
616                 spin_unlock_irq(&rtc_lock);
617                 return 0;
618         }
619 #ifdef RTC_IRQ
620         case RTC_IRQP_READ:     /* Read the periodic IRQ rate.  */
621         {
622                 return put_user(rtc_freq, (unsigned long __user *)arg);
623         }
624         case RTC_IRQP_SET:      /* Set periodic IRQ rate.       */
625         {
626                 int tmp = 0;
627                 unsigned char val;
628                 unsigned long flags; /* can be called from isr via rtc_control() */
629
630                 /* 
631                  * The max we can do is 8192Hz.
632                  */
633                 if ((arg < 2) || (arg > 8192))
634                         return -EINVAL;
635                 /*
636                  * We don't really want Joe User generating more
637                  * than 64Hz of interrupts on a multi-user machine.
638                  */
639                 if (!kernel && (arg > rtc_max_user_freq) && (!capable(CAP_SYS_RESOURCE)))
640                         return -EACCES;
641
642                 while (arg > (1<<tmp))
643                         tmp++;
644
645                 /*
646                  * Check that the input was really a power of 2.
647                  */
648                 if (arg != (1<<tmp))
649                         return -EINVAL;
650
651                 spin_lock_irqsave(&rtc_lock, flags);
652                 if (hpet_set_periodic_freq(arg)) {
653                         spin_unlock_irqrestore(&rtc_lock, flags);
654                         return 0;
655                 }
656                 rtc_freq = arg;
657
658                 val = CMOS_READ(RTC_FREQ_SELECT) & 0xf0;
659                 val |= (16 - tmp);
660                 CMOS_WRITE(val, RTC_FREQ_SELECT);
661                 spin_unlock_irqrestore(&rtc_lock, flags);
662                 return 0;
663         }
664 #endif
665         case RTC_EPOCH_READ:    /* Read the epoch.      */
666         {
667                 return put_user (epoch, (unsigned long __user *)arg);
668         }
669         case RTC_EPOCH_SET:     /* Set the epoch.       */
670         {
671                 /* 
672                  * There were no RTC clocks before 1900.
673                  */
674                 if (arg < 1900)
675                         return -EINVAL;
676
677                 if (!capable(CAP_SYS_TIME))
678                         return -EACCES;
679
680                 epoch = arg;
681                 return 0;
682         }
683         default:
684                 return -ENOTTY;
685         }
686         return copy_to_user((void __user *)arg, &wtime, sizeof wtime) ? -EFAULT : 0;
687 }
688
689 static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
690                      unsigned long arg)
691 {
692         return rtc_do_ioctl(cmd, arg, 0);
693 }
694
695 /*
696  *      We enforce only one user at a time here with the open/close.
697  *      Also clear the previous interrupt data on an open, and clean
698  *      up things on a close.
699  */
700
701 /* We use rtc_lock to protect against concurrent opens. So the BKL is not
702  * needed here. Or anywhere else in this driver. */
703 static int rtc_open(struct inode *inode, struct file *file)
704 {
705         spin_lock_irq (&rtc_lock);
706
707         if(rtc_status & RTC_IS_OPEN)
708                 goto out_busy;
709
710         rtc_status |= RTC_IS_OPEN;
711
712         rtc_irq_data = 0;
713         spin_unlock_irq (&rtc_lock);
714         return 0;
715
716 out_busy:
717         spin_unlock_irq (&rtc_lock);
718         return -EBUSY;
719 }
720
721 static int rtc_fasync (int fd, struct file *filp, int on)
722
723 {
724         return fasync_helper (fd, filp, on, &rtc_async_queue);
725 }
726
727 static int rtc_release(struct inode *inode, struct file *file)
728 {
729 #ifdef RTC_IRQ
730         unsigned char tmp;
731
732         if (rtc_has_irq == 0)
733                 goto no_irq;
734
735         /*
736          * Turn off all interrupts once the device is no longer
737          * in use, and clear the data.
738          */
739
740         spin_lock_irq(&rtc_lock);
741         if (!hpet_mask_rtc_irq_bit(RTC_PIE | RTC_AIE | RTC_UIE)) {
742                 tmp = CMOS_READ(RTC_CONTROL);
743                 tmp &=  ~RTC_PIE;
744                 tmp &=  ~RTC_AIE;
745                 tmp &=  ~RTC_UIE;
746                 CMOS_WRITE(tmp, RTC_CONTROL);
747                 CMOS_READ(RTC_INTR_FLAGS);
748         }
749         if (rtc_status & RTC_TIMER_ON) {
750                 rtc_status &= ~RTC_TIMER_ON;
751                 del_timer(&rtc_irq_timer);
752         }
753         spin_unlock_irq(&rtc_lock);
754
755         if (file->f_flags & FASYNC) {
756                 rtc_fasync (-1, file, 0);
757         }
758 no_irq:
759 #endif
760
761         spin_lock_irq (&rtc_lock);
762         rtc_irq_data = 0;
763         rtc_status &= ~RTC_IS_OPEN;
764         spin_unlock_irq (&rtc_lock);
765         return 0;
766 }
767
768 #ifdef RTC_IRQ
769 /* Called without the kernel lock - fine */
770 static unsigned int rtc_poll(struct file *file, poll_table *wait)
771 {
772         unsigned long l;
773
774         if (rtc_has_irq == 0)
775                 return 0;
776
777         poll_wait(file, &rtc_wait, wait);
778
779         spin_lock_irq (&rtc_lock);
780         l = rtc_irq_data;
781         spin_unlock_irq (&rtc_lock);
782
783         if (l != 0)
784                 return POLLIN | POLLRDNORM;
785         return 0;
786 }
787 #endif
788
789 /*
790  * exported stuffs
791  */
792
793 EXPORT_SYMBOL(rtc_register);
794 EXPORT_SYMBOL(rtc_unregister);
795 EXPORT_SYMBOL(rtc_control);
796
797 int rtc_register(rtc_task_t *task)
798 {
799 #ifndef RTC_IRQ
800         return -EIO;
801 #else
802         if (task == NULL || task->func == NULL)
803                 return -EINVAL;
804         spin_lock_irq(&rtc_lock);
805         if (rtc_status & RTC_IS_OPEN) {
806                 spin_unlock_irq(&rtc_lock);
807                 return -EBUSY;
808         }
809         spin_lock(&rtc_task_lock);
810         if (rtc_callback) {
811                 spin_unlock(&rtc_task_lock);
812                 spin_unlock_irq(&rtc_lock);
813                 return -EBUSY;
814         }
815         rtc_status |= RTC_IS_OPEN;
816         rtc_callback = task;
817         spin_unlock(&rtc_task_lock);
818         spin_unlock_irq(&rtc_lock);
819         return 0;
820 #endif
821 }
822
823 int rtc_unregister(rtc_task_t *task)
824 {
825 #ifndef RTC_IRQ
826         return -EIO;
827 #else
828         unsigned char tmp;
829
830         spin_lock_irq(&rtc_lock);
831         spin_lock(&rtc_task_lock);
832         if (rtc_callback != task) {
833                 spin_unlock(&rtc_task_lock);
834                 spin_unlock_irq(&rtc_lock);
835                 return -ENXIO;
836         }
837         rtc_callback = NULL;
838         
839         /* disable controls */
840         if (!hpet_mask_rtc_irq_bit(RTC_PIE | RTC_AIE | RTC_UIE)) {
841                 tmp = CMOS_READ(RTC_CONTROL);
842                 tmp &= ~RTC_PIE;
843                 tmp &= ~RTC_AIE;
844                 tmp &= ~RTC_UIE;
845                 CMOS_WRITE(tmp, RTC_CONTROL);
846                 CMOS_READ(RTC_INTR_FLAGS);
847         }
848         if (rtc_status & RTC_TIMER_ON) {
849                 rtc_status &= ~RTC_TIMER_ON;
850                 del_timer(&rtc_irq_timer);
851         }
852         rtc_status &= ~RTC_IS_OPEN;
853         spin_unlock(&rtc_task_lock);
854         spin_unlock_irq(&rtc_lock);
855         return 0;
856 #endif
857 }
858
859 int rtc_control(rtc_task_t *task, unsigned int cmd, unsigned long arg)
860 {
861 #ifndef RTC_IRQ
862         return -EIO;
863 #else
864         unsigned long flags;
865         if (cmd != RTC_PIE_ON && cmd != RTC_PIE_OFF && cmd != RTC_IRQP_SET)
866                 return -EINVAL;
867         spin_lock_irqsave(&rtc_task_lock, flags);
868         if (rtc_callback != task) {
869                 spin_unlock_irqrestore(&rtc_task_lock, flags);
870                 return -ENXIO;
871         }
872         spin_unlock_irqrestore(&rtc_task_lock, flags);
873         return rtc_do_ioctl(cmd, arg, 1);
874 #endif
875 }
876
877
878 /*
879  *      The various file operations we support.
880  */
881
882 static struct file_operations rtc_fops = {
883         .owner          = THIS_MODULE,
884         .llseek         = no_llseek,
885         .read           = rtc_read,
886 #ifdef RTC_IRQ
887         .poll           = rtc_poll,
888 #endif
889         .ioctl          = rtc_ioctl,
890         .open           = rtc_open,
891         .release        = rtc_release,
892         .fasync         = rtc_fasync,
893 };
894
895 static struct miscdevice rtc_dev = {
896         .minor          = RTC_MINOR,
897         .name           = "rtc",
898         .fops           = &rtc_fops,
899 };
900
901 static struct file_operations rtc_proc_fops = {
902         .owner = THIS_MODULE,
903         .open = rtc_proc_open,
904         .read  = seq_read,
905         .llseek = seq_lseek,
906         .release = single_release,
907 };
908
909 #if defined(RTC_IRQ) && !defined(__sparc__)
910 static irqreturn_t (*rtc_int_handler_ptr)(int irq, void *dev_id, struct pt_regs *regs);
911 #endif
912
913 static int __init rtc_init(void)
914 {
915         struct proc_dir_entry *ent;
916 #if defined(__alpha__) || defined(__mips__)
917         unsigned int year, ctrl;
918         char *guess = NULL;
919 #endif
920 #ifdef __sparc__
921         struct linux_ebus *ebus;
922         struct linux_ebus_device *edev;
923 #ifdef __sparc_v9__
924         struct sparc_isa_bridge *isa_br;
925         struct sparc_isa_device *isa_dev;
926 #endif
927 #endif
928
929 #ifdef __sparc__
930         for_each_ebus(ebus) {
931                 for_each_ebusdev(edev, ebus) {
932                         if(strcmp(edev->prom_name, "rtc") == 0) {
933                                 rtc_port = edev->resource[0].start;
934                                 rtc_irq = edev->irqs[0];
935                                 goto found;
936                         }
937                 }
938         }
939 #ifdef __sparc_v9__
940         for_each_isa(isa_br) {
941                 for_each_isadev(isa_dev, isa_br) {
942                         if (strcmp(isa_dev->prom_name, "rtc") == 0) {
943                                 rtc_port = isa_dev->resource.start;
944                                 rtc_irq = isa_dev->irq;
945                                 goto found;
946                         }
947                 }
948         }
949 #endif
950         printk(KERN_ERR "rtc_init: no PC rtc found\n");
951         return -EIO;
952
953 found:
954         if (rtc_irq == PCI_IRQ_NONE) {
955                 rtc_has_irq = 0;
956                 goto no_irq;
957         }
958
959         /*
960          * XXX Interrupt pin #7 in Espresso is shared between RTC and
961          * PCI Slot 2 INTA# (and some INTx# in Slot 1).
962          */
963         if (request_irq(rtc_irq, rtc_interrupt, SA_SHIRQ, "rtc", (void *)&rtc_port)) {
964                 /*
965                  * Standard way for sparc to print irq's is to use
966                  * __irq_itoa(). I think for EBus it's ok to use %d.
967                  */
968                 printk(KERN_ERR "rtc: cannot register IRQ %d\n", rtc_irq);
969                 return -EIO;
970         }
971 no_irq:
972 #else
973         if (!request_region(RTC_PORT(0), RTC_IO_EXTENT, "rtc")) {
974                 printk(KERN_ERR "rtc: I/O port %d is not free.\n", RTC_PORT (0));
975                 return -EIO;
976         }
977
978 #ifdef RTC_IRQ
979         if (is_hpet_enabled()) {
980                 rtc_int_handler_ptr = hpet_rtc_interrupt;
981         } else {
982                 rtc_int_handler_ptr = rtc_interrupt;
983         }
984
985         if(request_irq(RTC_IRQ, rtc_int_handler_ptr, SA_INTERRUPT, "rtc", NULL)) {
986                 /* Yeah right, seeing as irq 8 doesn't even hit the bus. */
987                 printk(KERN_ERR "rtc: IRQ %d is not free.\n", RTC_IRQ);
988                 release_region(RTC_PORT(0), RTC_IO_EXTENT);
989                 return -EIO;
990         }
991         hpet_rtc_timer_init();
992
993 #endif
994
995 #endif /* __sparc__ vs. others */
996
997         if (misc_register(&rtc_dev)) {
998 #ifdef RTC_IRQ
999                 free_irq(RTC_IRQ, NULL);
1000 #endif
1001                 release_region(RTC_PORT(0), RTC_IO_EXTENT);
1002                 return -ENODEV;
1003         }
1004
1005         ent = create_proc_entry("driver/rtc", 0, NULL);
1006         if (!ent) {
1007 #ifdef RTC_IRQ
1008                 free_irq(RTC_IRQ, NULL);
1009 #endif
1010                 release_region(RTC_PORT(0), RTC_IO_EXTENT);
1011                 misc_deregister(&rtc_dev);
1012                 return -ENOMEM;
1013         }
1014         ent->proc_fops = &rtc_proc_fops;
1015
1016 #if defined(__alpha__) || defined(__mips__)
1017         rtc_freq = HZ;
1018         
1019         /* Each operating system on an Alpha uses its own epoch.
1020            Let's try to guess which one we are using now. */
1021         
1022         if (rtc_is_updating() != 0)
1023                 msleep(20);
1024         
1025         spin_lock_irq(&rtc_lock);
1026         year = CMOS_READ(RTC_YEAR);
1027         ctrl = CMOS_READ(RTC_CONTROL);
1028         spin_unlock_irq(&rtc_lock);
1029         
1030         if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
1031                 BCD_TO_BIN(year);       /* This should never happen... */
1032         
1033         if (year < 20) {
1034                 epoch = 2000;
1035                 guess = "SRM (post-2000)";
1036         } else if (year >= 20 && year < 48) {
1037                 epoch = 1980;
1038                 guess = "ARC console";
1039         } else if (year >= 48 && year < 72) {
1040                 epoch = 1952;
1041                 guess = "Digital UNIX";
1042 #if defined(__mips__)
1043         } else if (year >= 72 && year < 74) {
1044                 epoch = 2000;
1045                 guess = "Digital DECstation";
1046 #else
1047         } else if (year >= 70) {
1048                 epoch = 1900;
1049                 guess = "Standard PC (1900)";
1050 #endif
1051         }
1052         if (guess)
1053                 printk(KERN_INFO "rtc: %s epoch (%lu) detected\n", guess, epoch);
1054 #endif
1055 #ifdef RTC_IRQ
1056         if (rtc_has_irq == 0)
1057                 goto no_irq2;
1058
1059         init_timer(&rtc_irq_timer);
1060         rtc_irq_timer.function = rtc_dropped_irq;
1061         spin_lock_irq(&rtc_lock);
1062         rtc_freq = 1024;
1063         if (!hpet_set_periodic_freq(rtc_freq)) {
1064                 /* Initialize periodic freq. to CMOS reset default, which is 1024Hz */
1065                 CMOS_WRITE(((CMOS_READ(RTC_FREQ_SELECT) & 0xF0) | 0x06), RTC_FREQ_SELECT);
1066         }
1067         spin_unlock_irq(&rtc_lock);
1068 no_irq2:
1069 #endif
1070
1071         (void) init_sysctl();
1072
1073         printk(KERN_INFO "Real Time Clock Driver v" RTC_VERSION "\n");
1074
1075         return 0;
1076 }
1077
1078 static void __exit rtc_exit (void)
1079 {
1080         cleanup_sysctl();
1081         remove_proc_entry ("driver/rtc", NULL);
1082         misc_deregister(&rtc_dev);
1083
1084 #ifdef __sparc__
1085         if (rtc_has_irq)
1086                 free_irq (rtc_irq, &rtc_port);
1087 #else
1088         release_region (RTC_PORT (0), RTC_IO_EXTENT);
1089 #ifdef RTC_IRQ
1090         if (rtc_has_irq)
1091                 free_irq (RTC_IRQ, NULL);
1092 #endif
1093 #endif /* __sparc__ */
1094 }
1095
1096 module_init(rtc_init);
1097 module_exit(rtc_exit);
1098
1099 #ifdef RTC_IRQ
1100 /*
1101  *      At IRQ rates >= 4096Hz, an interrupt may get lost altogether.
1102  *      (usually during an IDE disk interrupt, with IRQ unmasking off)
1103  *      Since the interrupt handler doesn't get called, the IRQ status
1104  *      byte doesn't get read, and the RTC stops generating interrupts.
1105  *      A timer is set, and will call this function if/when that happens.
1106  *      To get it out of this stalled state, we just read the status.
1107  *      At least a jiffy of interrupts (rtc_freq/HZ) will have been lost.
1108  *      (You *really* shouldn't be trying to use a non-realtime system 
1109  *      for something that requires a steady > 1KHz signal anyways.)
1110  */
1111
1112 static void rtc_dropped_irq(unsigned long data)
1113 {
1114         unsigned long freq;
1115
1116         spin_lock_irq (&rtc_lock);
1117
1118         if (hpet_rtc_dropped_irq()) {
1119                 spin_unlock_irq(&rtc_lock);
1120                 return;
1121         }
1122
1123         /* Just in case someone disabled the timer from behind our back... */
1124         if (rtc_status & RTC_TIMER_ON)
1125                 mod_timer(&rtc_irq_timer, jiffies + HZ/rtc_freq + 2*HZ/100);
1126
1127         rtc_irq_data += ((rtc_freq/HZ)<<8);
1128         rtc_irq_data &= ~0xff;
1129         rtc_irq_data |= (CMOS_READ(RTC_INTR_FLAGS) & 0xF0);     /* restart */
1130
1131         freq = rtc_freq;
1132
1133         spin_unlock_irq(&rtc_lock);
1134
1135         printk(KERN_WARNING "rtc: lost some interrupts at %ldHz.\n", freq);
1136
1137         /* Now we have new data */
1138         wake_up_interruptible(&rtc_wait);
1139
1140         kill_fasync (&rtc_async_queue, SIGIO, POLL_IN);
1141 }
1142 #endif
1143
1144 /*
1145  *      Info exported via "/proc/driver/rtc".
1146  */
1147
1148 static int rtc_proc_show(struct seq_file *seq, void *v)
1149 {
1150 #define YN(bit) ((ctrl & bit) ? "yes" : "no")
1151 #define NY(bit) ((ctrl & bit) ? "no" : "yes")
1152         struct rtc_time tm;
1153         unsigned char batt, ctrl;
1154         unsigned long freq;
1155
1156         spin_lock_irq(&rtc_lock);
1157         batt = CMOS_READ(RTC_VALID) & RTC_VRT;
1158         ctrl = CMOS_READ(RTC_CONTROL);
1159         freq = rtc_freq;
1160         spin_unlock_irq(&rtc_lock);
1161
1162
1163         rtc_get_rtc_time(&tm);
1164
1165         /*
1166          * There is no way to tell if the luser has the RTC set for local
1167          * time or for Universal Standard Time (GMT). Probably local though.
1168          */
1169         seq_printf(seq,
1170                    "rtc_time\t: %02d:%02d:%02d\n"
1171                    "rtc_date\t: %04d-%02d-%02d\n"
1172                    "rtc_epoch\t: %04lu\n",
1173                    tm.tm_hour, tm.tm_min, tm.tm_sec,
1174                    tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, epoch);
1175
1176         get_rtc_alm_time(&tm);
1177
1178         /*
1179          * We implicitly assume 24hr mode here. Alarm values >= 0xc0 will
1180          * match any value for that particular field. Values that are
1181          * greater than a valid time, but less than 0xc0 shouldn't appear.
1182          */
1183         seq_puts(seq, "alarm\t\t: ");
1184         if (tm.tm_hour <= 24)
1185                 seq_printf(seq, "%02d:", tm.tm_hour);
1186         else
1187                 seq_puts(seq, "**:");
1188
1189         if (tm.tm_min <= 59)
1190                 seq_printf(seq, "%02d:", tm.tm_min);
1191         else
1192                 seq_puts(seq, "**:");
1193
1194         if (tm.tm_sec <= 59)
1195                 seq_printf(seq, "%02d\n", tm.tm_sec);
1196         else
1197                 seq_puts(seq, "**\n");
1198
1199         seq_printf(seq,
1200                    "DST_enable\t: %s\n"
1201                    "BCD\t\t: %s\n"
1202                    "24hr\t\t: %s\n"
1203                    "square_wave\t: %s\n"
1204                    "alarm_IRQ\t: %s\n"
1205                    "update_IRQ\t: %s\n"
1206                    "periodic_IRQ\t: %s\n"
1207                    "periodic_freq\t: %ld\n"
1208                    "batt_status\t: %s\n",
1209                    YN(RTC_DST_EN),
1210                    NY(RTC_DM_BINARY),
1211                    YN(RTC_24H),
1212                    YN(RTC_SQWE),
1213                    YN(RTC_AIE),
1214                    YN(RTC_UIE),
1215                    YN(RTC_PIE),
1216                    freq,
1217                    batt ? "okay" : "dead");
1218
1219         return  0;
1220 #undef YN
1221 #undef NY
1222 }
1223
1224 static int rtc_proc_open(struct inode *inode, struct file *file)
1225 {
1226         return single_open(file, rtc_proc_show, NULL);
1227 }
1228
1229 void rtc_get_rtc_time(struct rtc_time *rtc_tm)
1230 {
1231         unsigned long uip_watchdog = jiffies;
1232         unsigned char ctrl;
1233 #ifdef CONFIG_MACH_DECSTATION
1234         unsigned int real_year;
1235 #endif
1236
1237         /*
1238          * read RTC once any update in progress is done. The update
1239          * can take just over 2ms. We wait 20ms. There is no need to
1240          * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
1241          * If you need to know *exactly* when a second has started, enable
1242          * periodic update complete interrupts, (via ioctl) and then 
1243          * immediately read /dev/rtc which will block until you get the IRQ.
1244          * Once the read clears, read the RTC time (again via ioctl). Easy.
1245          */
1246
1247         while (rtc_is_updating() != 0 && jiffies - uip_watchdog < 2*HZ/100) {
1248                 barrier();
1249                 cpu_relax();
1250         }
1251
1252         /*
1253          * Only the values that we read from the RTC are set. We leave
1254          * tm_wday, tm_yday and tm_isdst untouched. Note that while the
1255          * RTC has RTC_DAY_OF_WEEK, we should usually ignore it, as it is
1256          * only updated by the RTC when initially set to a non-zero value.
1257          */
1258         spin_lock_irq(&rtc_lock);
1259         rtc_tm->tm_sec = CMOS_READ(RTC_SECONDS);
1260         rtc_tm->tm_min = CMOS_READ(RTC_MINUTES);
1261         rtc_tm->tm_hour = CMOS_READ(RTC_HOURS);
1262         rtc_tm->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH);
1263         rtc_tm->tm_mon = CMOS_READ(RTC_MONTH);
1264         rtc_tm->tm_year = CMOS_READ(RTC_YEAR);
1265         /* Only set from 2.6.16 onwards */
1266         rtc_tm->tm_wday = CMOS_READ(RTC_DAY_OF_WEEK);
1267
1268 #ifdef CONFIG_MACH_DECSTATION
1269         real_year = CMOS_READ(RTC_DEC_YEAR);
1270 #endif
1271         ctrl = CMOS_READ(RTC_CONTROL);
1272         spin_unlock_irq(&rtc_lock);
1273
1274         if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
1275         {
1276                 BCD_TO_BIN(rtc_tm->tm_sec);
1277                 BCD_TO_BIN(rtc_tm->tm_min);
1278                 BCD_TO_BIN(rtc_tm->tm_hour);
1279                 BCD_TO_BIN(rtc_tm->tm_mday);
1280                 BCD_TO_BIN(rtc_tm->tm_mon);
1281                 BCD_TO_BIN(rtc_tm->tm_year);
1282                 BCD_TO_BIN(rtc_tm->tm_wday);
1283         }
1284
1285 #ifdef CONFIG_MACH_DECSTATION
1286         rtc_tm->tm_year += real_year - 72;
1287 #endif
1288
1289         /*
1290          * Account for differences between how the RTC uses the values
1291          * and how they are defined in a struct rtc_time;
1292          */
1293         if ((rtc_tm->tm_year += (epoch - 1900)) <= 69)
1294                 rtc_tm->tm_year += 100;
1295
1296         rtc_tm->tm_mon--;
1297 }
1298
1299 static void get_rtc_alm_time(struct rtc_time *alm_tm)
1300 {
1301         unsigned char ctrl;
1302
1303         /*
1304          * Only the values that we read from the RTC are set. That
1305          * means only tm_hour, tm_min, and tm_sec.
1306          */
1307         spin_lock_irq(&rtc_lock);
1308         alm_tm->tm_sec = CMOS_READ(RTC_SECONDS_ALARM);
1309         alm_tm->tm_min = CMOS_READ(RTC_MINUTES_ALARM);
1310         alm_tm->tm_hour = CMOS_READ(RTC_HOURS_ALARM);
1311         ctrl = CMOS_READ(RTC_CONTROL);
1312         spin_unlock_irq(&rtc_lock);
1313
1314         if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
1315         {
1316                 BCD_TO_BIN(alm_tm->tm_sec);
1317                 BCD_TO_BIN(alm_tm->tm_min);
1318                 BCD_TO_BIN(alm_tm->tm_hour);
1319         }
1320 }
1321
1322 #ifdef RTC_IRQ
1323 /*
1324  * Used to disable/enable interrupts for any one of UIE, AIE, PIE.
1325  * Rumour has it that if you frob the interrupt enable/disable
1326  * bits in RTC_CONTROL, you should read RTC_INTR_FLAGS, to
1327  * ensure you actually start getting interrupts. Probably for
1328  * compatibility with older/broken chipset RTC implementations.
1329  * We also clear out any old irq data after an ioctl() that
1330  * meddles with the interrupt enable/disable bits.
1331  */
1332
1333 static void mask_rtc_irq_bit_locked(unsigned char bit)
1334 {
1335         unsigned char val;
1336
1337         if (hpet_mask_rtc_irq_bit(bit))
1338                 return;
1339         val = CMOS_READ(RTC_CONTROL);
1340         val &=  ~bit;
1341         CMOS_WRITE(val, RTC_CONTROL);
1342         CMOS_READ(RTC_INTR_FLAGS);
1343
1344         rtc_irq_data = 0;
1345 }
1346
1347 static void set_rtc_irq_bit_locked(unsigned char bit)
1348 {
1349         unsigned char val;
1350
1351         if (hpet_set_rtc_irq_bit(bit))
1352                 return;
1353         val = CMOS_READ(RTC_CONTROL);
1354         val |= bit;
1355         CMOS_WRITE(val, RTC_CONTROL);
1356         CMOS_READ(RTC_INTR_FLAGS);
1357
1358         rtc_irq_data = 0;
1359 }
1360 #endif
1361
1362 MODULE_AUTHOR("Paul Gortmaker");
1363 MODULE_LICENSE("GPL");
1364 MODULE_ALIAS_MISCDEV(RTC_MINOR);