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