Import 2.1.118
[davej-history.git] / drivers / char / rtc.c
blobc5b326083d29e3f13bc45f8ee753bcb85af6c579
1 /*
2 * Real Time Clock interface for Linux
4 * Copyright (C) 1996 Paul Gortmaker
6 * This driver allows use of the real time clock (built into
7 * nearly all computers) from user space. It exports the /dev/rtc
8 * interface supporting various ioctl() and also the /proc/rtc
9 * pseudo-file for status information.
11 * The ioctls can be used to set the interrupt behaviour and
12 * generation rate from the RTC via IRQ 8. Then the /dev/rtc
13 * interface can be used to make use of these timer interrupts,
14 * be they interval or alarm based.
16 * The /dev/rtc interface will block on reads until an interrupt
17 * has been received. If a RTC interrupt has already happened,
18 * it will output an unsigned long and then block. The output value
19 * contains the interrupt status in the low byte and the number of
20 * interrupts since the last read in the remaining high bytes. The
21 * /dev/rtc interface can also be used with the select(2) call.
23 * This program is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU General Public License
25 * as published by the Free Software Foundation; either version
26 * 2 of the License, or (at your option) any later version.
28 * Based on other minimal char device drivers, like Alan's
29 * watchdog, Ted's random, etc. etc.
31 * 1.07 Paul Gortmaker.
32 * 1.08 Miquel van Smoorenburg: disallow certain things on the
33 * DEC Alpha as the CMOS clock is also used for other things.
34 * 1.09 Nikita Schmidt: epoch support and some Alpha cleanup.
38 #define RTC_VERSION "1.09"
40 #define RTC_IRQ 8 /* Can't see this changing soon. */
41 #define RTC_IO_EXTENT 0x10 /* Only really two ports, but... */
44 * Note that *all* calls to CMOS_READ and CMOS_WRITE are done with
45 * interrupts disabled. Due to the index-port/data-port (0x70/0x71)
46 * design of the RTC, we don't want two different things trying to
47 * get to it at once. (e.g. the periodic 11 min sync from time.c vs.
48 * this driver.)
51 #include <linux/types.h>
52 #include <linux/errno.h>
53 #include <linux/miscdevice.h>
54 #include <linux/malloc.h>
55 #include <linux/ioport.h>
56 #include <linux/fcntl.h>
57 #include <linux/mc146818rtc.h>
58 #include <linux/init.h>
59 #include <linux/poll.h>
61 #include <asm/io.h>
62 #include <asm/uaccess.h>
63 #include <asm/system.h>
66 * We sponge a minor off of the misc major. No need slurping
67 * up another valuable major dev number for this. If you add
68 * an ioctl, make sure you don't conflict with SPARC's RTC
69 * ioctls.
72 static struct wait_queue *rtc_wait;
74 static struct timer_list rtc_irq_timer;
76 static long long rtc_llseek(struct file *file, loff_t offset, int origin);
78 static ssize_t rtc_read(struct file *file, char *buf,
79 size_t count, loff_t *ppos);
81 static int rtc_ioctl(struct inode *inode, struct file *file,
82 unsigned int cmd, unsigned long arg);
84 static unsigned int rtc_poll(struct file *file, poll_table *wait);
86 void get_rtc_time (struct rtc_time *rtc_tm);
87 void get_rtc_alm_time (struct rtc_time *alm_tm);
88 void rtc_dropped_irq(unsigned long data);
90 void set_rtc_irq_bit(unsigned char bit);
91 void mask_rtc_irq_bit(unsigned char bit);
93 static inline unsigned char rtc_is_updating(void);
96 * Bits in rtc_status. (7 bits of room for future expansion)
99 #define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
100 #define RTC_TIMER_ON 0x02 /* missed irq timer active */
102 unsigned char rtc_status = 0; /* bitmapped status byte. */
103 unsigned long rtc_freq = 0; /* Current periodic IRQ rate */
104 unsigned long rtc_irq_data = 0; /* our output to the world */
107 * If this driver ever becomes modularised, it will be really nice
108 * to make the epoch retain its value across module reload...
111 static unsigned long epoch = 1900; /* year corresponding to 0x00 */
113 unsigned char days_in_mo[] =
114 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
117 * A very tiny interrupt handler. It runs with SA_INTERRUPT set,
118 * so that there is no possibility of conflicting with the
119 * set_rtc_mmss() call that happens during some timer interrupts.
120 * (See ./arch/XXXX/kernel/time.c for the set_rtc_mmss() function.)
123 static void rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
126 * Can be an alarm interrupt, update complete interrupt,
127 * or a periodic interrupt. We store the status in the
128 * low byte and the number of interrupts received since
129 * the last read in the remainder of rtc_irq_data.
132 rtc_irq_data += 0x100;
133 rtc_irq_data &= ~0xff;
134 rtc_irq_data |= (CMOS_READ(RTC_INTR_FLAGS) & 0xF0);
135 wake_up_interruptible(&rtc_wait);
137 if (rtc_status & RTC_TIMER_ON)
138 mod_timer(&rtc_irq_timer, jiffies + HZ/rtc_freq + 2*HZ/100);
142 * Now all the various file operations that we export.
145 static long long rtc_llseek(struct file *file, loff_t offset, int origin)
147 return -ESPIPE;
150 static ssize_t rtc_read(struct file *file, char *buf,
151 size_t count, loff_t *ppos)
153 struct wait_queue wait = { current, NULL };
154 unsigned long data;
155 ssize_t retval;
157 if (count < sizeof(unsigned long))
158 return -EINVAL;
160 add_wait_queue(&rtc_wait, &wait);
162 current->state = TASK_INTERRUPTIBLE;
164 while ((data = xchg(&rtc_irq_data, 0)) == 0) {
165 if (file->f_flags & O_NONBLOCK) {
166 retval = -EAGAIN;
167 goto out;
169 if (signal_pending(current)) {
170 retval = -ERESTARTSYS;
171 goto out;
173 schedule();
176 retval = put_user(data, (unsigned long *)buf);
177 if (!retval)
178 retval = sizeof(unsigned long);
179 out:
180 current->state = TASK_RUNNING;
181 remove_wait_queue(&rtc_wait, &wait);
183 return retval;
186 static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
187 unsigned long arg)
190 unsigned long flags;
191 struct rtc_time wtime;
193 switch (cmd) {
194 case RTC_AIE_OFF: /* Mask alarm int. enab. bit */
196 mask_rtc_irq_bit(RTC_AIE);
197 return 0;
199 case RTC_AIE_ON: /* Allow alarm interrupts. */
201 set_rtc_irq_bit(RTC_AIE);
202 return 0;
204 case RTC_PIE_OFF: /* Mask periodic int. enab. bit */
206 mask_rtc_irq_bit(RTC_PIE);
207 if (rtc_status & RTC_TIMER_ON) {
208 del_timer(&rtc_irq_timer);
209 rtc_status &= ~RTC_TIMER_ON;
211 return 0;
213 case RTC_PIE_ON: /* Allow periodic ints */
217 * We don't really want Joe User enabling more
218 * than 64Hz of interrupts on a multi-user machine.
220 if ((rtc_freq > 64) && (!capable(CAP_SYS_RESOURCE)))
221 return -EACCES;
223 if (!(rtc_status & RTC_TIMER_ON)) {
224 rtc_status |= RTC_TIMER_ON;
225 rtc_irq_timer.expires = jiffies + HZ/rtc_freq + 2*HZ/100;
226 add_timer(&rtc_irq_timer);
228 set_rtc_irq_bit(RTC_PIE);
229 return 0;
231 case RTC_UIE_OFF: /* Mask ints from RTC updates. */
233 mask_rtc_irq_bit(RTC_UIE);
234 return 0;
236 case RTC_UIE_ON: /* Allow ints for RTC updates. */
238 set_rtc_irq_bit(RTC_UIE);
239 return 0;
241 case RTC_ALM_READ: /* Read the present alarm time */
244 * This returns a struct rtc_time. Reading >= 0xc0
245 * means "don't care" or "match all". Only the tm_hour,
246 * tm_min, and tm_sec values are filled in.
249 get_rtc_alm_time(&wtime);
250 break;
252 case RTC_ALM_SET: /* Store a time into the alarm */
255 * This expects a struct rtc_time. Writing 0xff means
256 * "don't care" or "match all". Only the tm_hour,
257 * tm_min and tm_sec are used.
259 unsigned char hrs, min, sec;
260 struct rtc_time alm_tm;
262 if (copy_from_user(&alm_tm, (struct rtc_time*)arg,
263 sizeof(struct rtc_time)))
264 return -EFAULT;
266 hrs = alm_tm.tm_hour;
267 min = alm_tm.tm_min;
268 sec = alm_tm.tm_sec;
270 if (hrs >= 24)
271 hrs = 0xff;
273 if (min >= 60)
274 min = 0xff;
276 if (sec >= 60)
277 sec = 0xff;
279 save_flags(flags);
280 cli();
281 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) ||
282 RTC_ALWAYS_BCD)
284 BIN_TO_BCD(sec);
285 BIN_TO_BCD(min);
286 BIN_TO_BCD(hrs);
288 CMOS_WRITE(hrs, RTC_HOURS_ALARM);
289 CMOS_WRITE(min, RTC_MINUTES_ALARM);
290 CMOS_WRITE(sec, RTC_SECONDS_ALARM);
291 restore_flags(flags);
293 return 0;
295 case RTC_RD_TIME: /* Read the time/date from RTC */
297 get_rtc_time(&wtime);
298 break;
300 case RTC_SET_TIME: /* Set the RTC */
302 struct rtc_time rtc_tm;
303 unsigned char mon, day, hrs, min, sec, leap_yr;
304 unsigned char save_control, save_freq_select;
305 unsigned int yrs;
306 unsigned long flags;
308 if (!capable(CAP_SYS_TIME))
309 return -EACCES;
311 if (copy_from_user(&rtc_tm, (struct rtc_time*)arg,
312 sizeof(struct rtc_time)))
313 return -EFAULT;
315 yrs = rtc_tm.tm_year + 1900;
316 mon = rtc_tm.tm_mon + 1; /* tm_mon starts at zero */
317 day = rtc_tm.tm_mday;
318 hrs = rtc_tm.tm_hour;
319 min = rtc_tm.tm_min;
320 sec = rtc_tm.tm_sec;
322 if (yrs < 1970)
323 return -EINVAL;
325 leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
327 if ((mon > 12) || (day == 0))
328 return -EINVAL;
330 if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
331 return -EINVAL;
333 if ((hrs >= 24) || (min >= 60) || (sec >= 60))
334 return -EINVAL;
336 if ((yrs -= epoch) > 255) /* They are unsigned */
337 return -EINVAL;
339 save_flags(flags);
340 cli();
341 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY)
342 || RTC_ALWAYS_BCD) {
343 if (yrs > 169) {
344 restore_flags(flags);
345 return -EINVAL;
347 if (yrs >= 100)
348 yrs -= 100;
350 BIN_TO_BCD(sec);
351 BIN_TO_BCD(min);
352 BIN_TO_BCD(hrs);
353 BIN_TO_BCD(day);
354 BIN_TO_BCD(mon);
355 BIN_TO_BCD(yrs);
358 save_control = CMOS_READ(RTC_CONTROL);
359 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
360 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
361 CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
363 CMOS_WRITE(yrs, RTC_YEAR);
364 CMOS_WRITE(mon, RTC_MONTH);
365 CMOS_WRITE(day, RTC_DAY_OF_MONTH);
366 CMOS_WRITE(hrs, RTC_HOURS);
367 CMOS_WRITE(min, RTC_MINUTES);
368 CMOS_WRITE(sec, RTC_SECONDS);
370 CMOS_WRITE(save_control, RTC_CONTROL);
371 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
373 restore_flags(flags);
374 return 0;
376 case RTC_IRQP_READ: /* Read the periodic IRQ rate. */
378 return put_user(rtc_freq, (unsigned long *)arg);
380 case RTC_IRQP_SET: /* Set periodic IRQ rate. */
382 int tmp = 0;
383 unsigned char val;
386 * The max we can do is 8192Hz.
388 if ((arg < 2) || (arg > 8192))
389 return -EINVAL;
391 * We don't really want Joe User generating more
392 * than 64Hz of interrupts on a multi-user machine.
394 if ((arg > 64) && (!capable(CAP_SYS_RESOURCE)))
395 return -EACCES;
397 while (arg > (1<<tmp))
398 tmp++;
401 * Check that the input was really a power of 2.
403 if (arg != (1<<tmp))
404 return -EINVAL;
406 rtc_freq = arg;
408 save_flags(flags);
409 cli();
410 val = CMOS_READ(RTC_FREQ_SELECT) & 0xf0;
411 val |= (16 - tmp);
412 CMOS_WRITE(val, RTC_FREQ_SELECT);
413 restore_flags(flags);
414 return 0;
416 #ifdef __alpha__
417 case RTC_EPOCH_READ: /* Read the epoch. */
419 return put_user (epoch, (unsigned long *)arg);
421 case RTC_EPOCH_SET: /* Set the epoch. */
424 * There were no RTC clocks before 1900.
426 if (arg < 1900)
427 return -EINVAL;
429 if (!capable(CAP_SYS_TIME))
430 return -EACCES;
432 epoch = arg;
433 return 0;
435 #endif
436 default:
437 return -EINVAL;
439 return copy_to_user((void *)arg, &wtime, sizeof wtime) ? -EFAULT : 0;
443 * We enforce only one user at a time here with the open/close.
444 * Also clear the previous interrupt data on an open, and clean
445 * up things on a close.
448 static int rtc_open(struct inode *inode, struct file *file)
450 if(rtc_status & RTC_IS_OPEN)
451 return -EBUSY;
453 rtc_status |= RTC_IS_OPEN;
454 rtc_irq_data = 0;
455 return 0;
458 static int rtc_release(struct inode *inode, struct file *file)
461 * Turn off all interrupts once the device is no longer
462 * in use, and clear the data.
465 unsigned char tmp;
466 unsigned long flags;
468 save_flags(flags);
469 cli();
470 tmp = CMOS_READ(RTC_CONTROL);
471 tmp &= ~RTC_PIE;
472 tmp &= ~RTC_AIE;
473 tmp &= ~RTC_UIE;
474 CMOS_WRITE(tmp, RTC_CONTROL);
475 CMOS_READ(RTC_INTR_FLAGS);
476 restore_flags(flags);
478 if (rtc_status & RTC_TIMER_ON) {
479 rtc_status &= ~RTC_TIMER_ON;
480 del_timer(&rtc_irq_timer);
483 rtc_irq_data = 0;
484 rtc_status &= ~RTC_IS_OPEN;
485 return 0;
488 static unsigned int rtc_poll(struct file *file, poll_table *wait)
490 poll_wait(file, &rtc_wait, wait);
491 if (rtc_irq_data != 0)
492 return POLLIN | POLLRDNORM;
493 return 0;
497 * The various file operations we support.
500 static struct file_operations rtc_fops = {
501 rtc_llseek,
502 rtc_read,
503 NULL, /* No write */
504 NULL, /* No readdir */
505 rtc_poll,
506 rtc_ioctl,
507 NULL, /* No mmap */
508 rtc_open,
509 NULL, /* flush */
510 rtc_release
513 static struct miscdevice rtc_dev=
515 RTC_MINOR,
516 "rtc",
517 &rtc_fops
520 __initfunc(int rtc_init(void))
522 unsigned long flags;
523 #ifdef __alpha__
524 unsigned int year, ctrl;
525 unsigned long uip_watchdog;
526 char *guess = NULL;
527 #endif
528 printk(KERN_INFO "Real Time Clock Driver v%s\n", RTC_VERSION);
529 if(request_irq(RTC_IRQ, rtc_interrupt, SA_INTERRUPT, "rtc", NULL))
531 /* Yeah right, seeing as irq 8 doesn't even hit the bus. */
532 printk(KERN_ERR "rtc: IRQ %d is not free.\n", RTC_IRQ);
533 return -EIO;
535 misc_register(&rtc_dev);
536 /* Check region? Naaah! Just snarf it up. */
537 request_region(RTC_PORT(0), RTC_IO_EXTENT, "rtc");
538 #ifdef __alpha__
539 rtc_freq = HZ;
541 /* Each operating system on an Alpha uses its own epoch.
542 Let's try to guess which one we are using now. */
544 uip_watchdog = jiffies;
545 if (rtc_is_updating() != 0)
546 while (jiffies - uip_watchdog < 2*HZ/100)
547 barrier();
549 save_flags(flags);
550 cli();
551 year = CMOS_READ(RTC_YEAR);
552 ctrl = CMOS_READ(RTC_CONTROL);
553 restore_flags(flags);
555 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
556 BCD_TO_BIN(year); /* This should never happen... */
558 if (year > 10 && year < 44) {
559 epoch = 1980;
560 guess = "ARC console";
561 } else if (year < 96) {
562 epoch = 1952;
563 guess = "Digital UNIX";
565 if (guess)
566 printk("rtc: %s epoch (%lu) detected\n", guess, epoch);
567 #endif
568 init_timer(&rtc_irq_timer);
569 rtc_irq_timer.function = rtc_dropped_irq;
570 rtc_wait = NULL;
571 save_flags(flags);
572 cli();
573 /* Initialize periodic freq. to CMOS reset default, which is 1024Hz */
574 CMOS_WRITE(((CMOS_READ(RTC_FREQ_SELECT) & 0xF0) | 0x06), RTC_FREQ_SELECT);
575 restore_flags(flags);
576 rtc_freq = 1024;
577 return 0;
581 * At IRQ rates >= 4096Hz, an interrupt may get lost altogether.
582 * (usually during an IDE disk interrupt, with IRQ unmasking off)
583 * Since the interrupt handler doesn't get called, the IRQ status
584 * byte doesn't get read, and the RTC stops generating interrupts.
585 * A timer is set, and will call this function if/when that happens.
586 * To get it out of this stalled state, we just read the status.
587 * At least a jiffy of interrupts (rtc_freq/HZ) will have been lost.
588 * (You *really* shouldn't be trying to use a non-realtime system
589 * for something that requires a steady > 1KHz signal anyways.)
592 void rtc_dropped_irq(unsigned long data)
594 unsigned long flags;
596 printk(KERN_INFO "rtc: lost some interrupts at %ldHz.\n", rtc_freq);
597 mod_timer(&rtc_irq_timer, jiffies + HZ/rtc_freq + 2*HZ/100);
599 save_flags(flags);
600 cli();
601 rtc_irq_data += ((rtc_freq/HZ)<<8);
602 rtc_irq_data &= ~0xff;
603 rtc_irq_data |= (CMOS_READ(RTC_INTR_FLAGS) & 0xF0); /* restart */
604 restore_flags(flags);
608 * Info exported via "/proc/rtc".
611 int get_rtc_status(char *buf)
613 char *p;
614 struct rtc_time tm;
615 unsigned char batt, ctrl;
616 unsigned long flags;
618 save_flags(flags);
619 cli();
620 batt = CMOS_READ(RTC_VALID) & RTC_VRT;
621 ctrl = CMOS_READ(RTC_CONTROL);
622 restore_flags(flags);
624 p = buf;
626 get_rtc_time(&tm);
629 * There is no way to tell if the luser has the RTC set for local
630 * time or for Universal Standard Time (GMT). Probably local though.
632 p += sprintf(p,
633 "rtc_time\t: %02d:%02d:%02d\n"
634 "rtc_date\t: %04d-%02d-%02d\n"
635 "rtc_epoch\t: %04lu\n",
636 tm.tm_hour, tm.tm_min, tm.tm_sec,
637 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, epoch);
639 get_rtc_alm_time(&tm);
642 * We implicitly assume 24hr mode here. Alarm values >= 0xc0 will
643 * match any value for that particular field. Values that are
644 * greater than a valid time, but less than 0xc0 shouldn't appear.
646 p += sprintf(p, "alarm\t\t: ");
647 if (tm.tm_hour <= 24)
648 p += sprintf(p, "%02d:", tm.tm_hour);
649 else
650 p += sprintf(p, "**:");
652 if (tm.tm_min <= 59)
653 p += sprintf(p, "%02d:", tm.tm_min);
654 else
655 p += sprintf(p, "**:");
657 if (tm.tm_sec <= 59)
658 p += sprintf(p, "%02d\n", tm.tm_sec);
659 else
660 p += sprintf(p, "**\n");
662 p += sprintf(p,
663 "DST_enable\t: %s\n"
664 "BCD\t\t: %s\n"
665 "24hr\t\t: %s\n"
666 "square_wave\t: %s\n"
667 "alarm_IRQ\t: %s\n"
668 "update_IRQ\t: %s\n"
669 "periodic_IRQ\t: %s\n"
670 "periodic_freq\t: %ld\n"
671 "batt_status\t: %s\n",
672 (ctrl & RTC_DST_EN) ? "yes" : "no",
673 (ctrl & RTC_DM_BINARY) ? "no" : "yes",
674 (ctrl & RTC_24H) ? "yes" : "no",
675 (ctrl & RTC_SQWE) ? "yes" : "no",
676 (ctrl & RTC_AIE) ? "yes" : "no",
677 (ctrl & RTC_UIE) ? "yes" : "no",
678 (ctrl & RTC_PIE) ? "yes" : "no",
679 rtc_freq,
680 batt ? "okay" : "dead");
682 return p - buf;
686 * Returns true if a clock update is in progress
688 static inline unsigned char rtc_is_updating(void)
690 unsigned long flags;
691 unsigned char uip;
693 save_flags(flags);
694 cli();
695 uip = (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP);
696 restore_flags(flags);
697 return uip;
700 void get_rtc_time(struct rtc_time *rtc_tm)
703 unsigned long flags, uip_watchdog = jiffies;
704 unsigned char ctrl;
707 * read RTC once any update in progress is done. The update
708 * can take just over 2ms. We wait 10 to 20ms. There is no need to
709 * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
710 * If you need to know *exactly* when a second has started, enable
711 * periodic update complete interrupts, (via ioctl) and then
712 * immediately read /dev/rtc which will block until you get the IRQ.
713 * Once the read clears, read the RTC time (again via ioctl). Easy.
716 if (rtc_is_updating() != 0)
717 while (jiffies - uip_watchdog < 2*HZ/100)
718 barrier();
721 * Only the values that we read from the RTC are set. We leave
722 * tm_wday, tm_yday and tm_isdst untouched. Even though the
723 * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
724 * by the RTC when initially set to a non-zero value.
726 save_flags(flags);
727 cli();
728 rtc_tm->tm_sec = CMOS_READ(RTC_SECONDS);
729 rtc_tm->tm_min = CMOS_READ(RTC_MINUTES);
730 rtc_tm->tm_hour = CMOS_READ(RTC_HOURS);
731 rtc_tm->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH);
732 rtc_tm->tm_mon = CMOS_READ(RTC_MONTH);
733 rtc_tm->tm_year = CMOS_READ(RTC_YEAR);
734 ctrl = CMOS_READ(RTC_CONTROL);
735 restore_flags(flags);
737 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
739 BCD_TO_BIN(rtc_tm->tm_sec);
740 BCD_TO_BIN(rtc_tm->tm_min);
741 BCD_TO_BIN(rtc_tm->tm_hour);
742 BCD_TO_BIN(rtc_tm->tm_mday);
743 BCD_TO_BIN(rtc_tm->tm_mon);
744 BCD_TO_BIN(rtc_tm->tm_year);
748 * Account for differences between how the RTC uses the values
749 * and how they are defined in a struct rtc_time;
751 if ((rtc_tm->tm_year += (epoch - 1900)) <= 69)
752 rtc_tm->tm_year += 100;
754 rtc_tm->tm_mon--;
757 void get_rtc_alm_time(struct rtc_time *alm_tm)
759 unsigned long flags;
760 unsigned char ctrl;
763 * Only the values that we read from the RTC are set. That
764 * means only tm_hour, tm_min, and tm_sec.
766 save_flags(flags);
767 cli();
768 alm_tm->tm_sec = CMOS_READ(RTC_SECONDS_ALARM);
769 alm_tm->tm_min = CMOS_READ(RTC_MINUTES_ALARM);
770 alm_tm->tm_hour = CMOS_READ(RTC_HOURS_ALARM);
771 ctrl = CMOS_READ(RTC_CONTROL);
772 restore_flags(flags);
774 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
776 BCD_TO_BIN(alm_tm->tm_sec);
777 BCD_TO_BIN(alm_tm->tm_min);
778 BCD_TO_BIN(alm_tm->tm_hour);
783 * Used to disable/enable interrupts for any one of UIE, AIE, PIE.
784 * Rumour has it that if you frob the interrupt enable/disable
785 * bits in RTC_CONTROL, you should read RTC_INTR_FLAGS, to
786 * ensure you actually start getting interrupts. Probably for
787 * compatibility with older/broken chipset RTC implementations.
788 * We also clear out any old irq data after an ioctl() that
789 * meddles with the interrupt enable/disable bits.
792 void mask_rtc_irq_bit(unsigned char bit)
794 unsigned char val;
795 unsigned long flags;
797 save_flags(flags);
798 cli();
799 val = CMOS_READ(RTC_CONTROL);
800 val &= ~bit;
801 CMOS_WRITE(val, RTC_CONTROL);
802 CMOS_READ(RTC_INTR_FLAGS);
803 restore_flags(flags);
804 rtc_irq_data = 0;
807 void set_rtc_irq_bit(unsigned char bit)
809 unsigned char val;
810 unsigned long flags;
812 save_flags(flags);
813 cli();
814 val = CMOS_READ(RTC_CONTROL);
815 val |= bit;
816 CMOS_WRITE(val, RTC_CONTROL);
817 CMOS_READ(RTC_INTR_FLAGS);
818 rtc_irq_data = 0;
819 restore_flags(flags);