Import 2.4.0-test2pre7
[davej-history.git] / drivers / char / rtc.c
blobf227b5094b5ada18be96d8102ccebed886a3a7f6
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
9 * /proc/driver/rtc pseudo-file for status information.
11 * The ioctls can be used to set the interrupt behaviour and
12 * generation rate from the RTC via IRQ 8. Then the /dev/rtc
13 * interface can be used to make use of these timer interrupts,
14 * be they interval or alarm based.
16 * The /dev/rtc interface will block on reads until an interrupt
17 * has been received. If a RTC interrupt has already happened,
18 * it will output an unsigned long and then block. The output value
19 * contains the interrupt status in the low byte and the number of
20 * interrupts since the last read in the remaining high bytes. The
21 * /dev/rtc interface can also be used with the select(2) call.
23 * This program is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU General Public License
25 * as published by the Free Software Foundation; either version
26 * 2 of the License, or (at your option) any later version.
28 * Based on other minimal char device drivers, like Alan's
29 * watchdog, Ted's random, etc. etc.
31 * 1.07 Paul Gortmaker.
32 * 1.08 Miquel van Smoorenburg: disallow certain things on the
33 * DEC Alpha as the CMOS clock is also used for other things.
34 * 1.09 Nikita Schmidt: epoch support and some Alpha cleanup.
35 * 1.09a Pete Zaitcev: Sun SPARC
36 * 1.09b Jeff Garzik: Modularize, init cleanup
37 * 1.09c Jeff Garzik: SMP cleanup
38 * 1.10 Paul Barton-Davis: add support for async I/O
39 * 1.10a Andrea Arcangeli: Alpha updates
40 * 1.10b Andrew Morton: SMP lock fix
41 * 1.10c Cesar Barros: SMP locking fixes and cleanup
44 #define RTC_VERSION "1.10c"
46 #define RTC_IRQ 8 /* Can't see this changing soon. */
47 #define RTC_IO_EXTENT 0x10 /* Only really two ports, but... */
50 * Note that *all* calls to CMOS_READ and CMOS_WRITE are done with
51 * interrupts disabled. Due to the index-port/data-port (0x70/0x71)
52 * design of the RTC, we don't want two different things trying to
53 * get to it at once. (e.g. the periodic 11 min sync from time.c vs.
54 * this driver.)
57 #include <linux/module.h>
58 #include <linux/kernel.h>
59 #include <linux/types.h>
60 #include <linux/miscdevice.h>
61 #include <linux/ioport.h>
62 #include <linux/fcntl.h>
63 #include <linux/mc146818rtc.h>
64 #include <linux/init.h>
65 #include <linux/poll.h>
66 #include <linux/proc_fs.h>
67 #include <linux/spinlock.h>
69 #include <asm/io.h>
70 #include <asm/uaccess.h>
71 #include <asm/system.h>
73 #ifdef __sparc__
74 #include <asm/ebus.h>
76 static unsigned long rtc_port;
77 static int rtc_irq;
78 #endif
81 * We sponge a minor off of the misc major. No need slurping
82 * up another valuable major dev number for this. If you add
83 * an ioctl, make sure you don't conflict with SPARC's RTC
84 * ioctls.
87 static struct fasync_struct *rtc_async_queue;
89 static DECLARE_WAIT_QUEUE_HEAD(rtc_wait);
91 extern spinlock_t rtc_lock;
93 static struct timer_list rtc_irq_timer;
95 static loff_t rtc_llseek(struct file *file, loff_t offset, int origin);
97 static ssize_t rtc_read(struct file *file, char *buf,
98 size_t count, loff_t *ppos);
100 static int rtc_ioctl(struct inode *inode, struct file *file,
101 unsigned int cmd, unsigned long arg);
103 #ifndef __alpha__
104 static unsigned int rtc_poll(struct file *file, poll_table *wait);
105 #endif
107 static void get_rtc_time (struct rtc_time *rtc_tm);
108 static void get_rtc_alm_time (struct rtc_time *alm_tm);
109 #ifndef __alpha__
110 static void rtc_dropped_irq(unsigned long data);
112 static void set_rtc_irq_bit(unsigned char bit);
113 static void mask_rtc_irq_bit(unsigned char bit);
114 #endif
116 static inline unsigned char rtc_is_updating(void);
118 static int rtc_read_proc(char *page, char **start, off_t off,
119 int count, int *eof, void *data);
122 * Bits in rtc_status. (6 bits of room for future expansion)
125 #define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
126 #define RTC_TIMER_ON 0x02 /* missed irq timer active */
129 * rtc_status is never changed by rtc_interrupt, and ioctl/open/close is
130 * protected by the big kernel lock. However, ioctl can still disable the timer
131 * in rtc_status and then with del_timer after the interrupt has read
132 * rtc_status but before mod_timer is called, which would then reenable the
133 * timer (but you would need to have an awful timing before you'd trip on it)
135 static unsigned long rtc_status = 0; /* bitmapped status byte. */
136 static unsigned long rtc_freq = 0; /* Current periodic IRQ rate */
137 static unsigned long rtc_irq_data = 0; /* our output to the world */
140 * If this driver ever becomes modularised, it will be really nice
141 * to make the epoch retain its value across module reload...
144 static unsigned long epoch = 1900; /* year corresponding to 0x00 */
146 static const unsigned char days_in_mo[] =
147 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
149 #ifndef __alpha__
151 * A very tiny interrupt handler. It runs with SA_INTERRUPT set,
152 * but there is possibility of conflicting with the set_rtc_mmss()
153 * call (the rtc irq and the timer irq can easily run at the same
154 * time in two different CPUs). So we need to serializes
155 * accesses to the chip with the rtc_lock spinlock that each
156 * architecture should implement in the timer code.
157 * (See ./arch/XXXX/kernel/time.c for the set_rtc_mmss() function.)
160 static void rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
163 * Can be an alarm interrupt, update complete interrupt,
164 * or a periodic interrupt. We store the status in the
165 * low byte and the number of interrupts received since
166 * the last read in the remainder of rtc_irq_data.
169 spin_lock (&rtc_lock);
170 rtc_irq_data += 0x100;
171 rtc_irq_data &= ~0xff;
172 rtc_irq_data |= (CMOS_READ(RTC_INTR_FLAGS) & 0xF0);
174 if (rtc_status & RTC_TIMER_ON)
175 mod_timer(&rtc_irq_timer, jiffies + HZ/rtc_freq + 2*HZ/100);
177 spin_unlock (&rtc_lock);
179 /* Now do the rest of the actions */
180 wake_up_interruptible(&rtc_wait);
182 kill_fasync (&rtc_async_queue, SIGIO, POLL_IN);
184 #endif
187 * Now all the various file operations that we export.
190 static loff_t rtc_llseek(struct file *file, loff_t offset, int origin)
192 return -ESPIPE;
195 static ssize_t rtc_read(struct file *file, char *buf,
196 size_t count, loff_t *ppos)
198 #ifdef __alpha__
199 return -EIO;
200 #else
201 DECLARE_WAITQUEUE(wait, current);
202 unsigned long data;
203 ssize_t retval;
205 if (count < sizeof(unsigned long))
206 return -EINVAL;
208 add_wait_queue(&rtc_wait, &wait);
210 current->state = TASK_INTERRUPTIBLE;
212 do {
213 /* First make it right. Then make it fast. Putting this whole
214 * block within the parentheses of a while would be too
215 * confusing. And no, xchg() is not the answer. */
216 spin_lock_irq (&rtc_lock);
217 data = rtc_irq_data;
218 rtc_irq_data = 0;
219 spin_unlock_irq (&rtc_lock);
221 if (data != 0)
222 break;
224 if (file->f_flags & O_NONBLOCK) {
225 retval = -EAGAIN;
226 goto out;
228 if (signal_pending(current)) {
229 retval = -ERESTARTSYS;
230 goto out;
232 schedule();
233 } while (1);
235 retval = put_user(data, (unsigned long *)buf);
236 if (!retval)
237 retval = sizeof(unsigned long);
238 out:
239 current->state = TASK_RUNNING;
240 remove_wait_queue(&rtc_wait, &wait);
242 return retval;
243 #endif
246 static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
247 unsigned long arg)
249 struct rtc_time wtime;
251 switch (cmd) {
252 #ifndef __alpha__
253 case RTC_AIE_OFF: /* Mask alarm int. enab. bit */
255 mask_rtc_irq_bit(RTC_AIE);
256 return 0;
258 case RTC_AIE_ON: /* Allow alarm interrupts. */
260 set_rtc_irq_bit(RTC_AIE);
261 return 0;
263 case RTC_PIE_OFF: /* Mask periodic int. enab. bit */
265 mask_rtc_irq_bit(RTC_PIE);
266 if (rtc_status & RTC_TIMER_ON) {
267 spin_lock_irq (&rtc_lock);
268 rtc_status &= ~RTC_TIMER_ON;
269 del_timer(&rtc_irq_timer);
270 spin_unlock_irq (&rtc_lock);
272 return 0;
274 case RTC_PIE_ON: /* Allow periodic ints */
278 * We don't really want Joe User enabling more
279 * than 64Hz of interrupts on a multi-user machine.
281 if ((rtc_freq > 64) && (!capable(CAP_SYS_RESOURCE)))
282 return -EACCES;
284 if (!(rtc_status & RTC_TIMER_ON)) {
285 spin_lock_irq (&rtc_lock);
286 rtc_irq_timer.expires = jiffies + HZ/rtc_freq + 2*HZ/100;
287 add_timer(&rtc_irq_timer);
288 rtc_status |= RTC_TIMER_ON;
289 spin_unlock_irq (&rtc_lock);
291 set_rtc_irq_bit(RTC_PIE);
292 return 0;
294 case RTC_UIE_OFF: /* Mask ints from RTC updates. */
296 mask_rtc_irq_bit(RTC_UIE);
297 return 0;
299 case RTC_UIE_ON: /* Allow ints for RTC updates. */
301 set_rtc_irq_bit(RTC_UIE);
302 return 0;
304 #endif
305 case RTC_ALM_READ: /* Read the present alarm time */
308 * This returns a struct rtc_time. Reading >= 0xc0
309 * means "don't care" or "match all". Only the tm_hour,
310 * tm_min, and tm_sec values are filled in.
313 get_rtc_alm_time(&wtime);
314 break;
316 case RTC_ALM_SET: /* Store a time into the alarm */
319 * This expects a struct rtc_time. Writing 0xff means
320 * "don't care" or "match all". Only the tm_hour,
321 * tm_min and tm_sec are used.
323 unsigned char hrs, min, sec;
324 struct rtc_time alm_tm;
326 if (copy_from_user(&alm_tm, (struct rtc_time*)arg,
327 sizeof(struct rtc_time)))
328 return -EFAULT;
330 hrs = alm_tm.tm_hour;
331 min = alm_tm.tm_min;
332 sec = alm_tm.tm_sec;
334 if (hrs >= 24)
335 hrs = 0xff;
337 if (min >= 60)
338 min = 0xff;
340 if (sec >= 60)
341 sec = 0xff;
343 spin_lock_irq(&rtc_lock);
344 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) ||
345 RTC_ALWAYS_BCD)
347 BIN_TO_BCD(sec);
348 BIN_TO_BCD(min);
349 BIN_TO_BCD(hrs);
351 CMOS_WRITE(hrs, RTC_HOURS_ALARM);
352 CMOS_WRITE(min, RTC_MINUTES_ALARM);
353 CMOS_WRITE(sec, RTC_SECONDS_ALARM);
354 spin_unlock_irq(&rtc_lock);
356 return 0;
358 case RTC_RD_TIME: /* Read the time/date from RTC */
360 get_rtc_time(&wtime);
361 break;
363 case RTC_SET_TIME: /* Set the RTC */
365 struct rtc_time rtc_tm;
366 unsigned char mon, day, hrs, min, sec, leap_yr;
367 unsigned char save_control, save_freq_select;
368 unsigned int yrs;
370 if (!capable(CAP_SYS_TIME))
371 return -EACCES;
373 if (copy_from_user(&rtc_tm, (struct rtc_time*)arg,
374 sizeof(struct rtc_time)))
375 return -EFAULT;
377 yrs = rtc_tm.tm_year + 1900;
378 mon = rtc_tm.tm_mon + 1; /* tm_mon starts at zero */
379 day = rtc_tm.tm_mday;
380 hrs = rtc_tm.tm_hour;
381 min = rtc_tm.tm_min;
382 sec = rtc_tm.tm_sec;
384 if (yrs < 1970)
385 return -EINVAL;
387 leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
389 if ((mon > 12) || (day == 0))
390 return -EINVAL;
392 if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
393 return -EINVAL;
395 if ((hrs >= 24) || (min >= 60) || (sec >= 60))
396 return -EINVAL;
398 if ((yrs -= epoch) > 255) /* They are unsigned */
399 return -EINVAL;
401 spin_lock_irq(&rtc_lock);
402 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY)
403 || RTC_ALWAYS_BCD) {
404 if (yrs > 169) {
405 spin_unlock_irq(&rtc_lock);
406 return -EINVAL;
408 if (yrs >= 100)
409 yrs -= 100;
411 BIN_TO_BCD(sec);
412 BIN_TO_BCD(min);
413 BIN_TO_BCD(hrs);
414 BIN_TO_BCD(day);
415 BIN_TO_BCD(mon);
416 BIN_TO_BCD(yrs);
419 save_control = CMOS_READ(RTC_CONTROL);
420 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
421 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
422 CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
424 CMOS_WRITE(yrs, RTC_YEAR);
425 CMOS_WRITE(mon, RTC_MONTH);
426 CMOS_WRITE(day, RTC_DAY_OF_MONTH);
427 CMOS_WRITE(hrs, RTC_HOURS);
428 CMOS_WRITE(min, RTC_MINUTES);
429 CMOS_WRITE(sec, RTC_SECONDS);
431 CMOS_WRITE(save_control, RTC_CONTROL);
432 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
434 spin_unlock_irq(&rtc_lock);
435 return 0;
437 #ifndef __alpha__
438 case RTC_IRQP_READ: /* Read the periodic IRQ rate. */
440 return put_user(rtc_freq, (unsigned long *)arg);
442 case RTC_IRQP_SET: /* Set periodic IRQ rate. */
444 int tmp = 0;
445 unsigned char val;
448 * The max we can do is 8192Hz.
450 if ((arg < 2) || (arg > 8192))
451 return -EINVAL;
453 * We don't really want Joe User generating more
454 * than 64Hz of interrupts on a multi-user machine.
456 if ((arg > 64) && (!capable(CAP_SYS_RESOURCE)))
457 return -EACCES;
459 while (arg > (1<<tmp))
460 tmp++;
463 * Check that the input was really a power of 2.
465 if (arg != (1<<tmp))
466 return -EINVAL;
468 spin_lock_irq(&rtc_lock);
469 rtc_freq = arg;
471 val = CMOS_READ(RTC_FREQ_SELECT) & 0xf0;
472 val |= (16 - tmp);
473 CMOS_WRITE(val, RTC_FREQ_SELECT);
474 spin_unlock_irq(&rtc_lock);
475 return 0;
477 #else
478 case RTC_EPOCH_READ: /* Read the epoch. */
480 return put_user (epoch, (unsigned long *)arg);
482 case RTC_EPOCH_SET: /* Set the epoch. */
485 * There were no RTC clocks before 1900.
487 if (arg < 1900)
488 return -EINVAL;
490 if (!capable(CAP_SYS_TIME))
491 return -EACCES;
493 epoch = arg;
494 return 0;
496 #endif
497 default:
498 return -EINVAL;
500 return copy_to_user((void *)arg, &wtime, sizeof wtime) ? -EFAULT : 0;
504 * We enforce only one user at a time here with the open/close.
505 * Also clear the previous interrupt data on an open, and clean
506 * up things on a close.
509 static int rtc_open(struct inode *inode, struct file *file)
511 /* If someday somebody decides to remove the kernel_lock on open and
512 * close and ioctl this is gonna get open to races */
513 if(rtc_status & RTC_IS_OPEN)
514 return -EBUSY;
516 rtc_status |= RTC_IS_OPEN;
518 spin_lock_irq (&rtc_lock);
519 rtc_irq_data = 0;
520 spin_unlock_irq (&rtc_lock);
521 return 0;
524 static int rtc_fasync (int fd, struct file *filp, int on)
527 return fasync_helper (fd, filp, on, &rtc_async_queue);
530 static int rtc_release(struct inode *inode, struct file *file)
532 #ifndef __alpha__
534 * Turn off all interrupts once the device is no longer
535 * in use, and clear the data.
538 unsigned char tmp;
540 spin_lock_irq(&rtc_lock);
541 tmp = CMOS_READ(RTC_CONTROL);
542 tmp &= ~RTC_PIE;
543 tmp &= ~RTC_AIE;
544 tmp &= ~RTC_UIE;
545 CMOS_WRITE(tmp, RTC_CONTROL);
546 CMOS_READ(RTC_INTR_FLAGS);
548 if (rtc_status & RTC_TIMER_ON) {
549 rtc_status &= ~RTC_TIMER_ON;
550 del_timer(&rtc_irq_timer);
552 spin_unlock_irq(&rtc_lock);
554 if (file->f_flags & FASYNC) {
555 rtc_fasync (-1, file, 0);
558 #endif
560 spin_lock_irq (&rtc_lock);
561 rtc_irq_data = 0;
562 spin_unlock_irq (&rtc_lock);
563 rtc_status = rtc_status & ~RTC_IS_OPEN;
564 return 0;
567 #ifndef __alpha__
568 /* Called without the kernel lock - fine */
569 static unsigned int rtc_poll(struct file *file, poll_table *wait)
571 unsigned long l;
573 poll_wait(file, &rtc_wait, wait);
575 spin_lock_irq (&rtc_lock);
576 l = rtc_irq_data;
577 spin_unlock_irq (&rtc_lock);
579 if (l != 0)
580 return POLLIN | POLLRDNORM;
581 return 0;
583 #endif
586 * The various file operations we support.
589 static struct file_operations rtc_fops = {
590 owner: THIS_MODULE,
591 llseek: rtc_llseek,
592 read: rtc_read,
593 #ifndef __alpha__
594 poll: rtc_poll,
595 #endif
596 ioctl: rtc_ioctl,
597 open: rtc_open,
598 release: rtc_release,
599 fasync: rtc_fasync,
602 static struct miscdevice rtc_dev=
604 RTC_MINOR,
605 "rtc",
606 &rtc_fops
609 static int __init rtc_init(void)
611 #ifdef __alpha__
612 unsigned int year, ctrl;
613 unsigned long uip_watchdog;
614 char *guess = NULL;
615 #endif
616 #ifdef __sparc__
617 struct linux_ebus *ebus;
618 struct linux_ebus_device *edev;
619 #endif
621 #ifdef __sparc__
622 for_each_ebus(ebus) {
623 for_each_ebusdev(edev, ebus) {
624 if(strcmp(edev->prom_name, "rtc") == 0) {
625 goto found;
629 printk("rtc_init: no PC rtc found\n");
630 return -EIO;
632 found:
633 rtc_port = edev->resource[0].start;
634 rtc_irq = edev->irqs[0];
636 * XXX Interrupt pin #7 in Espresso is shared between RTC and
637 * PCI Slot 2 INTA# (and some INTx# in Slot 1). SA_INTERRUPT here
638 * is asking for trouble with add-on boards. Change to SA_SHIRQ.
640 if(request_irq(rtc_irq, rtc_interrupt, SA_INTERRUPT, "rtc", (void *)&rtc_port)) {
642 * Standard way for sparc to print irq's is to use
643 * __irq_itoa(). I think for EBus it's ok to use %d.
645 printk("rtc: cannot register IRQ %d\n", rtc_irq);
646 return -EIO;
648 #else
649 if (check_region (RTC_PORT (0), RTC_IO_EXTENT))
651 printk(KERN_ERR "rtc: I/O port %d is not free.\n", RTC_PORT (0));
652 return -EIO;
655 #ifndef __alpha__
656 if(request_irq(RTC_IRQ, rtc_interrupt, SA_INTERRUPT, "rtc", NULL))
658 /* Yeah right, seeing as irq 8 doesn't even hit the bus. */
659 printk(KERN_ERR "rtc: IRQ %d is not free.\n", RTC_IRQ);
660 return -EIO;
662 #endif
664 request_region(RTC_PORT(0), RTC_IO_EXTENT, "rtc");
665 #endif /* __sparc__ vs. others */
667 misc_register(&rtc_dev);
668 create_proc_read_entry ("driver/rtc", 0, 0, rtc_read_proc, NULL);
670 #ifdef __alpha__
671 rtc_freq = HZ;
673 /* Each operating system on an Alpha uses its own epoch.
674 Let's try to guess which one we are using now. */
676 uip_watchdog = jiffies;
677 if (rtc_is_updating() != 0)
678 while (jiffies - uip_watchdog < 2*HZ/100)
679 barrier();
681 spin_lock_irq(&rtc_lock);
682 year = CMOS_READ(RTC_YEAR);
683 ctrl = CMOS_READ(RTC_CONTROL);
684 spin_unlock_irq(&rtc_lock);
686 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
687 BCD_TO_BIN(year); /* This should never happen... */
689 if (year >= 20 && year < 48) {
690 epoch = 1980;
691 guess = "ARC console";
692 } else if (year >= 48 && year < 100) {
693 epoch = 1952;
694 guess = "Digital UNIX";
696 if (guess)
697 printk("rtc: %s epoch (%lu) detected\n", guess, epoch);
698 #endif
699 #ifndef __alpha__
700 init_timer(&rtc_irq_timer);
701 rtc_irq_timer.function = rtc_dropped_irq;
702 spin_lock_irq(&rtc_lock);
703 /* Initialize periodic freq. to CMOS reset default, which is 1024Hz */
704 CMOS_WRITE(((CMOS_READ(RTC_FREQ_SELECT) & 0xF0) | 0x06), RTC_FREQ_SELECT);
705 spin_unlock_irq(&rtc_lock);
706 rtc_freq = 1024;
707 #endif
709 printk(KERN_INFO "Real Time Clock Driver v" RTC_VERSION "\n");
711 return 0;
714 static void __exit rtc_exit (void)
716 /* interrupts and maybe timer disabled at this point by rtc_release */
717 /* FIXME: Maybe??? */
719 if (rtc_status & RTC_TIMER_ON) {
720 spin_lock_irq (&rtc_lock);
721 rtc_status &= ~RTC_TIMER_ON;
722 del_timer(&rtc_irq_timer);
723 spin_unlock_irq (&rtc_lock);
725 printk(KERN_WARNING "rtc_exit(), and timer still running.\n");
728 remove_proc_entry ("driver/rtc", NULL);
729 misc_deregister(&rtc_dev);
731 #ifdef __sparc__
732 free_irq (rtc_irq, &rtc_port);
733 #else
734 release_region (RTC_PORT (0), RTC_IO_EXTENT);
735 #ifndef __alpha__
736 free_irq (RTC_IRQ, NULL);
737 #endif
738 #endif /* __sparc__ */
741 module_init(rtc_init);
742 module_exit(rtc_exit);
743 EXPORT_NO_SYMBOLS;
745 #ifndef __alpha__
747 * At IRQ rates >= 4096Hz, an interrupt may get lost altogether.
748 * (usually during an IDE disk interrupt, with IRQ unmasking off)
749 * Since the interrupt handler doesn't get called, the IRQ status
750 * byte doesn't get read, and the RTC stops generating interrupts.
751 * A timer is set, and will call this function if/when that happens.
752 * To get it out of this stalled state, we just read the status.
753 * At least a jiffy of interrupts (rtc_freq/HZ) will have been lost.
754 * (You *really* shouldn't be trying to use a non-realtime system
755 * for something that requires a steady > 1KHz signal anyways.)
758 static void rtc_dropped_irq(unsigned long data)
760 unsigned long freq;
762 spin_lock_irq (&rtc_lock);
764 /* Just in case someone disabled the timer from behind our back... */
765 if (rtc_status & RTC_TIMER_ON)
766 mod_timer(&rtc_irq_timer, jiffies + HZ/rtc_freq + 2*HZ/100);
768 rtc_irq_data += ((rtc_freq/HZ)<<8);
769 rtc_irq_data &= ~0xff;
770 rtc_irq_data |= (CMOS_READ(RTC_INTR_FLAGS) & 0xF0); /* restart */
772 freq = rtc_freq;
774 spin_unlock_irq(&rtc_lock);
776 printk(KERN_INFO "rtc: lost some interrupts at %ldHz.\n", freq);
778 /* Now we have new data */
779 wake_up_interruptible(&rtc_wait);
781 kill_fasync (&rtc_async_queue, SIGIO, POLL_IN);
783 #endif
786 * Info exported via "/proc/driver/rtc".
789 static int rtc_proc_output (char *buf)
791 #define YN(bit) ((ctrl & bit) ? "yes" : "no")
792 #define NY(bit) ((ctrl & bit) ? "no" : "yes")
793 char *p;
794 struct rtc_time tm;
795 unsigned char batt, ctrl;
796 unsigned long freq;
798 spin_lock_irq(&rtc_lock);
799 batt = CMOS_READ(RTC_VALID) & RTC_VRT;
800 ctrl = CMOS_READ(RTC_CONTROL);
801 freq = rtc_freq;
802 spin_unlock_irq(&rtc_lock);
804 p = buf;
806 get_rtc_time(&tm);
809 * There is no way to tell if the luser has the RTC set for local
810 * time or for Universal Standard Time (GMT). Probably local though.
812 p += sprintf(p,
813 "rtc_time\t: %02d:%02d:%02d\n"
814 "rtc_date\t: %04d-%02d-%02d\n"
815 "rtc_epoch\t: %04lu\n",
816 tm.tm_hour, tm.tm_min, tm.tm_sec,
817 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, epoch);
819 get_rtc_alm_time(&tm);
822 * We implicitly assume 24hr mode here. Alarm values >= 0xc0 will
823 * match any value for that particular field. Values that are
824 * greater than a valid time, but less than 0xc0 shouldn't appear.
826 p += sprintf(p, "alarm\t\t: ");
827 if (tm.tm_hour <= 24)
828 p += sprintf(p, "%02d:", tm.tm_hour);
829 else
830 p += sprintf(p, "**:");
832 if (tm.tm_min <= 59)
833 p += sprintf(p, "%02d:", tm.tm_min);
834 else
835 p += sprintf(p, "**:");
837 if (tm.tm_sec <= 59)
838 p += sprintf(p, "%02d\n", tm.tm_sec);
839 else
840 p += sprintf(p, "**\n");
842 p += sprintf(p,
843 "DST_enable\t: %s\n"
844 "BCD\t\t: %s\n"
845 "24hr\t\t: %s\n"
846 "square_wave\t: %s\n"
847 "alarm_IRQ\t: %s\n"
848 "update_IRQ\t: %s\n"
849 "periodic_IRQ\t: %s\n"
850 "periodic_freq\t: %ld\n"
851 "batt_status\t: %s\n",
852 YN(RTC_DST_EN),
853 NY(RTC_DM_BINARY),
854 YN(RTC_24H),
855 YN(RTC_SQWE),
856 YN(RTC_AIE),
857 YN(RTC_UIE),
858 YN(RTC_PIE),
859 freq,
860 batt ? "okay" : "dead");
862 return p - buf;
863 #undef YN
864 #undef NY
867 static int rtc_read_proc(char *page, char **start, off_t off,
868 int count, int *eof, void *data)
870 int len = rtc_proc_output (page);
871 if (len <= off+count) *eof = 1;
872 *start = page + off;
873 len -= off;
874 if (len>count) len = count;
875 if (len<0) len = 0;
876 return len;
880 * Returns true if a clock update is in progress
882 /* FIXME shouldn't this be above rtc_init to make it fully inlined? */
883 static inline unsigned char rtc_is_updating(void)
885 unsigned char uip;
887 spin_lock_irq(&rtc_lock);
888 uip = (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP);
889 spin_unlock_irq(&rtc_lock);
890 return uip;
893 static void get_rtc_time(struct rtc_time *rtc_tm)
895 unsigned long uip_watchdog = jiffies;
896 unsigned char ctrl;
899 * read RTC once any update in progress is done. The update
900 * can take just over 2ms. We wait 10 to 20ms. There is no need to
901 * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
902 * If you need to know *exactly* when a second has started, enable
903 * periodic update complete interrupts, (via ioctl) and then
904 * immediately read /dev/rtc which will block until you get the IRQ.
905 * Once the read clears, read the RTC time (again via ioctl). Easy.
908 if (rtc_is_updating() != 0)
909 while (jiffies - uip_watchdog < 2*HZ/100)
910 barrier();
913 * Only the values that we read from the RTC are set. We leave
914 * tm_wday, tm_yday and tm_isdst untouched. Even though the
915 * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
916 * by the RTC when initially set to a non-zero value.
918 spin_lock_irq(&rtc_lock);
919 rtc_tm->tm_sec = CMOS_READ(RTC_SECONDS);
920 rtc_tm->tm_min = CMOS_READ(RTC_MINUTES);
921 rtc_tm->tm_hour = CMOS_READ(RTC_HOURS);
922 rtc_tm->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH);
923 rtc_tm->tm_mon = CMOS_READ(RTC_MONTH);
924 rtc_tm->tm_year = CMOS_READ(RTC_YEAR);
925 ctrl = CMOS_READ(RTC_CONTROL);
926 spin_unlock_irq(&rtc_lock);
928 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
930 BCD_TO_BIN(rtc_tm->tm_sec);
931 BCD_TO_BIN(rtc_tm->tm_min);
932 BCD_TO_BIN(rtc_tm->tm_hour);
933 BCD_TO_BIN(rtc_tm->tm_mday);
934 BCD_TO_BIN(rtc_tm->tm_mon);
935 BCD_TO_BIN(rtc_tm->tm_year);
939 * Account for differences between how the RTC uses the values
940 * and how they are defined in a struct rtc_time;
942 if ((rtc_tm->tm_year += (epoch - 1900)) <= 69)
943 rtc_tm->tm_year += 100;
945 rtc_tm->tm_mon--;
948 static void get_rtc_alm_time(struct rtc_time *alm_tm)
950 unsigned char ctrl;
953 * Only the values that we read from the RTC are set. That
954 * means only tm_hour, tm_min, and tm_sec.
956 spin_lock_irq(&rtc_lock);
957 alm_tm->tm_sec = CMOS_READ(RTC_SECONDS_ALARM);
958 alm_tm->tm_min = CMOS_READ(RTC_MINUTES_ALARM);
959 alm_tm->tm_hour = CMOS_READ(RTC_HOURS_ALARM);
960 ctrl = CMOS_READ(RTC_CONTROL);
961 spin_unlock_irq(&rtc_lock);
963 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
965 BCD_TO_BIN(alm_tm->tm_sec);
966 BCD_TO_BIN(alm_tm->tm_min);
967 BCD_TO_BIN(alm_tm->tm_hour);
971 #ifndef __alpha__
973 * Used to disable/enable interrupts for any one of UIE, AIE, PIE.
974 * Rumour has it that if you frob the interrupt enable/disable
975 * bits in RTC_CONTROL, you should read RTC_INTR_FLAGS, to
976 * ensure you actually start getting interrupts. Probably for
977 * compatibility with older/broken chipset RTC implementations.
978 * We also clear out any old irq data after an ioctl() that
979 * meddles with the interrupt enable/disable bits.
982 static void mask_rtc_irq_bit(unsigned char bit)
984 unsigned char val;
986 spin_lock_irq(&rtc_lock);
987 val = CMOS_READ(RTC_CONTROL);
988 val &= ~bit;
989 CMOS_WRITE(val, RTC_CONTROL);
990 CMOS_READ(RTC_INTR_FLAGS);
992 rtc_irq_data = 0;
993 spin_unlock_irq(&rtc_lock);
996 static void set_rtc_irq_bit(unsigned char bit)
998 unsigned char val;
1000 spin_lock_irq(&rtc_lock);
1001 val = CMOS_READ(RTC_CONTROL);
1002 val |= bit;
1003 CMOS_WRITE(val, RTC_CONTROL);
1004 CMOS_READ(RTC_INTR_FLAGS);
1006 rtc_irq_data = 0;
1007 spin_unlock_irq(&rtc_lock);
1009 #endif