- pre1:
[davej-history.git] / drivers / char / rtc.c
blob029f5df658e052671d88f98afdb4173df8810a98
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
42 * 1.10d Paul Gortmaker: delete paranoia check in rtc_exit
45 #define RTC_VERSION "1.10d"
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/config.h>
58 #include <linux/module.h>
59 #include <linux/kernel.h>
60 #include <linux/types.h>
61 #include <linux/miscdevice.h>
62 #include <linux/ioport.h>
63 #include <linux/fcntl.h>
64 #include <linux/mc146818rtc.h>
65 #include <linux/init.h>
66 #include <linux/poll.h>
67 #include <linux/proc_fs.h>
68 #include <linux/spinlock.h>
70 #include <asm/io.h>
71 #include <asm/uaccess.h>
72 #include <asm/system.h>
74 #ifdef __sparc__
75 #include <asm/ebus.h>
77 static unsigned long rtc_port;
78 static int rtc_irq;
79 #endif
82 * We sponge a minor off of the misc major. No need slurping
83 * up another valuable major dev number for this. If you add
84 * an ioctl, make sure you don't conflict with SPARC's RTC
85 * ioctls.
88 static struct fasync_struct *rtc_async_queue;
90 static DECLARE_WAIT_QUEUE_HEAD(rtc_wait);
92 extern spinlock_t rtc_lock;
94 static struct timer_list rtc_irq_timer;
96 static loff_t rtc_llseek(struct file *file, loff_t offset, int origin);
98 static ssize_t rtc_read(struct file *file, char *buf,
99 size_t count, loff_t *ppos);
101 static int rtc_ioctl(struct inode *inode, struct file *file,
102 unsigned int cmd, unsigned long arg);
104 #if RTC_IRQ
105 static unsigned int rtc_poll(struct file *file, poll_table *wait);
106 #endif
108 static void get_rtc_time (struct rtc_time *rtc_tm);
109 static void get_rtc_alm_time (struct rtc_time *alm_tm);
110 #if RTC_IRQ
111 static void rtc_dropped_irq(unsigned long data);
113 static void set_rtc_irq_bit(unsigned char bit);
114 static void mask_rtc_irq_bit(unsigned char bit);
115 #endif
117 static inline unsigned char rtc_is_updating(void);
119 static int rtc_read_proc(char *page, char **start, off_t off,
120 int count, int *eof, void *data);
123 * Bits in rtc_status. (6 bits of room for future expansion)
126 #define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
127 #define RTC_TIMER_ON 0x02 /* missed irq timer active */
130 * rtc_status is never changed by rtc_interrupt, and ioctl/open/close is
131 * protected by the big kernel lock. However, ioctl can still disable the timer
132 * in rtc_status and then with del_timer after the interrupt has read
133 * rtc_status but before mod_timer is called, which would then reenable the
134 * timer (but you would need to have an awful timing before you'd trip on it)
136 static unsigned long rtc_status = 0; /* bitmapped status byte. */
137 static unsigned long rtc_freq = 0; /* Current periodic IRQ rate */
138 static unsigned long rtc_irq_data = 0; /* our output to the world */
141 * If this driver ever becomes modularised, it will be really nice
142 * to make the epoch retain its value across module reload...
145 static unsigned long epoch = 1900; /* year corresponding to 0x00 */
147 static const unsigned char days_in_mo[] =
148 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
150 #if RTC_IRQ
152 * A very tiny interrupt handler. It runs with SA_INTERRUPT set,
153 * but there is possibility of conflicting with the set_rtc_mmss()
154 * call (the rtc irq and the timer irq can easily run at the same
155 * time in two different CPUs). So we need to serializes
156 * accesses to the chip with the rtc_lock spinlock that each
157 * architecture should implement in the timer code.
158 * (See ./arch/XXXX/kernel/time.c for the set_rtc_mmss() function.)
161 static void rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
164 * Can be an alarm interrupt, update complete interrupt,
165 * or a periodic interrupt. We store the status in the
166 * low byte and the number of interrupts received since
167 * the last read in the remainder of rtc_irq_data.
170 spin_lock (&rtc_lock);
171 rtc_irq_data += 0x100;
172 rtc_irq_data &= ~0xff;
173 rtc_irq_data |= (CMOS_READ(RTC_INTR_FLAGS) & 0xF0);
175 if (rtc_status & RTC_TIMER_ON)
176 mod_timer(&rtc_irq_timer, jiffies + HZ/rtc_freq + 2*HZ/100);
178 spin_unlock (&rtc_lock);
180 /* Now do the rest of the actions */
181 wake_up_interruptible(&rtc_wait);
183 kill_fasync (&rtc_async_queue, SIGIO, POLL_IN);
185 #endif
188 * Now all the various file operations that we export.
191 static loff_t rtc_llseek(struct file *file, loff_t offset, int origin)
193 return -ESPIPE;
196 static ssize_t rtc_read(struct file *file, char *buf,
197 size_t count, loff_t *ppos)
199 #if !RTC_IRQ
200 return -EIO;
201 #else
202 DECLARE_WAITQUEUE(wait, current);
203 unsigned long data;
204 ssize_t retval;
206 if (count < sizeof(unsigned long))
207 return -EINVAL;
209 add_wait_queue(&rtc_wait, &wait);
211 current->state = TASK_INTERRUPTIBLE;
213 do {
214 /* First make it right. Then make it fast. Putting this whole
215 * block within the parentheses of a while would be too
216 * confusing. And no, xchg() is not the answer. */
217 spin_lock_irq (&rtc_lock);
218 data = rtc_irq_data;
219 rtc_irq_data = 0;
220 spin_unlock_irq (&rtc_lock);
222 if (data != 0)
223 break;
225 if (file->f_flags & O_NONBLOCK) {
226 retval = -EAGAIN;
227 goto out;
229 if (signal_pending(current)) {
230 retval = -ERESTARTSYS;
231 goto out;
233 schedule();
234 } while (1);
236 retval = put_user(data, (unsigned long *)buf);
237 if (!retval)
238 retval = sizeof(unsigned long);
239 out:
240 current->state = TASK_RUNNING;
241 remove_wait_queue(&rtc_wait, &wait);
243 return retval;
244 #endif
247 static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
248 unsigned long arg)
250 struct rtc_time wtime;
252 switch (cmd) {
253 #if RTC_IRQ
254 case RTC_AIE_OFF: /* Mask alarm int. enab. bit */
256 mask_rtc_irq_bit(RTC_AIE);
257 return 0;
259 case RTC_AIE_ON: /* Allow alarm interrupts. */
261 set_rtc_irq_bit(RTC_AIE);
262 return 0;
264 case RTC_PIE_OFF: /* Mask periodic int. enab. bit */
266 mask_rtc_irq_bit(RTC_PIE);
267 if (rtc_status & RTC_TIMER_ON) {
268 spin_lock_irq (&rtc_lock);
269 rtc_status &= ~RTC_TIMER_ON;
270 del_timer(&rtc_irq_timer);
271 spin_unlock_irq (&rtc_lock);
273 return 0;
275 case RTC_PIE_ON: /* Allow periodic ints */
279 * We don't really want Joe User enabling more
280 * than 64Hz of interrupts on a multi-user machine.
282 if ((rtc_freq > 64) && (!capable(CAP_SYS_RESOURCE)))
283 return -EACCES;
285 if (!(rtc_status & RTC_TIMER_ON)) {
286 spin_lock_irq (&rtc_lock);
287 rtc_irq_timer.expires = jiffies + HZ/rtc_freq + 2*HZ/100;
288 add_timer(&rtc_irq_timer);
289 rtc_status |= RTC_TIMER_ON;
290 spin_unlock_irq (&rtc_lock);
292 set_rtc_irq_bit(RTC_PIE);
293 return 0;
295 case RTC_UIE_OFF: /* Mask ints from RTC updates. */
297 mask_rtc_irq_bit(RTC_UIE);
298 return 0;
300 case RTC_UIE_ON: /* Allow ints for RTC updates. */
302 set_rtc_irq_bit(RTC_UIE);
303 return 0;
305 #endif
306 case RTC_ALM_READ: /* Read the present alarm time */
309 * This returns a struct rtc_time. Reading >= 0xc0
310 * means "don't care" or "match all". Only the tm_hour,
311 * tm_min, and tm_sec values are filled in.
314 get_rtc_alm_time(&wtime);
315 break;
317 case RTC_ALM_SET: /* Store a time into the alarm */
320 * This expects a struct rtc_time. Writing 0xff means
321 * "don't care" or "match all". Only the tm_hour,
322 * tm_min and tm_sec are used.
324 unsigned char hrs, min, sec;
325 struct rtc_time alm_tm;
327 if (copy_from_user(&alm_tm, (struct rtc_time*)arg,
328 sizeof(struct rtc_time)))
329 return -EFAULT;
331 hrs = alm_tm.tm_hour;
332 min = alm_tm.tm_min;
333 sec = alm_tm.tm_sec;
335 if (hrs >= 24)
336 hrs = 0xff;
338 if (min >= 60)
339 min = 0xff;
341 if (sec >= 60)
342 sec = 0xff;
344 spin_lock_irq(&rtc_lock);
345 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) ||
346 RTC_ALWAYS_BCD)
348 BIN_TO_BCD(sec);
349 BIN_TO_BCD(min);
350 BIN_TO_BCD(hrs);
352 CMOS_WRITE(hrs, RTC_HOURS_ALARM);
353 CMOS_WRITE(min, RTC_MINUTES_ALARM);
354 CMOS_WRITE(sec, RTC_SECONDS_ALARM);
355 spin_unlock_irq(&rtc_lock);
357 return 0;
359 case RTC_RD_TIME: /* Read the time/date from RTC */
361 get_rtc_time(&wtime);
362 break;
364 case RTC_SET_TIME: /* Set the RTC */
366 struct rtc_time rtc_tm;
367 unsigned char mon, day, hrs, min, sec, leap_yr;
368 unsigned char save_control, save_freq_select;
369 unsigned int yrs;
371 if (!capable(CAP_SYS_TIME))
372 return -EACCES;
374 if (copy_from_user(&rtc_tm, (struct rtc_time*)arg,
375 sizeof(struct rtc_time)))
376 return -EFAULT;
378 yrs = rtc_tm.tm_year + 1900;
379 mon = rtc_tm.tm_mon + 1; /* tm_mon starts at zero */
380 day = rtc_tm.tm_mday;
381 hrs = rtc_tm.tm_hour;
382 min = rtc_tm.tm_min;
383 sec = rtc_tm.tm_sec;
385 if (yrs < 1970)
386 return -EINVAL;
388 leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
390 if ((mon > 12) || (day == 0))
391 return -EINVAL;
393 if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
394 return -EINVAL;
396 if ((hrs >= 24) || (min >= 60) || (sec >= 60))
397 return -EINVAL;
399 if ((yrs -= epoch) > 255) /* They are unsigned */
400 return -EINVAL;
402 spin_lock_irq(&rtc_lock);
403 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY)
404 || RTC_ALWAYS_BCD) {
405 if (yrs > 169) {
406 spin_unlock_irq(&rtc_lock);
407 return -EINVAL;
409 if (yrs >= 100)
410 yrs -= 100;
412 BIN_TO_BCD(sec);
413 BIN_TO_BCD(min);
414 BIN_TO_BCD(hrs);
415 BIN_TO_BCD(day);
416 BIN_TO_BCD(mon);
417 BIN_TO_BCD(yrs);
420 save_control = CMOS_READ(RTC_CONTROL);
421 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
422 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
423 CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
425 CMOS_WRITE(yrs, RTC_YEAR);
426 CMOS_WRITE(mon, RTC_MONTH);
427 CMOS_WRITE(day, RTC_DAY_OF_MONTH);
428 CMOS_WRITE(hrs, RTC_HOURS);
429 CMOS_WRITE(min, RTC_MINUTES);
430 CMOS_WRITE(sec, RTC_SECONDS);
432 CMOS_WRITE(save_control, RTC_CONTROL);
433 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
435 spin_unlock_irq(&rtc_lock);
436 return 0;
438 #if RTC_IRQ
439 case RTC_IRQP_READ: /* Read the periodic IRQ rate. */
441 return put_user(rtc_freq, (unsigned long *)arg);
443 case RTC_IRQP_SET: /* Set periodic IRQ rate. */
445 int tmp = 0;
446 unsigned char val;
449 * The max we can do is 8192Hz.
451 if ((arg < 2) || (arg > 8192))
452 return -EINVAL;
454 * We don't really want Joe User generating more
455 * than 64Hz of interrupts on a multi-user machine.
457 if ((arg > 64) && (!capable(CAP_SYS_RESOURCE)))
458 return -EACCES;
460 while (arg > (1<<tmp))
461 tmp++;
464 * Check that the input was really a power of 2.
466 if (arg != (1<<tmp))
467 return -EINVAL;
469 spin_lock_irq(&rtc_lock);
470 rtc_freq = arg;
472 val = CMOS_READ(RTC_FREQ_SELECT) & 0xf0;
473 val |= (16 - tmp);
474 CMOS_WRITE(val, RTC_FREQ_SELECT);
475 spin_unlock_irq(&rtc_lock);
476 return 0;
478 #elif !defined(CONFIG_DECSTATION)
479 case RTC_EPOCH_READ: /* Read the epoch. */
481 return put_user (epoch, (unsigned long *)arg);
483 case RTC_EPOCH_SET: /* Set the epoch. */
486 * There were no RTC clocks before 1900.
488 if (arg < 1900)
489 return -EINVAL;
491 if (!capable(CAP_SYS_TIME))
492 return -EACCES;
494 epoch = arg;
495 return 0;
497 #endif
498 default:
499 return -EINVAL;
501 return copy_to_user((void *)arg, &wtime, sizeof wtime) ? -EFAULT : 0;
505 * We enforce only one user at a time here with the open/close.
506 * Also clear the previous interrupt data on an open, and clean
507 * up things on a close.
510 /* We use rtc_lock to protect against concurrent opens. So the BKL is not
511 * needed here. Or anywhere else in this driver. */
512 static int rtc_open(struct inode *inode, struct file *file)
514 spin_lock_irq (&rtc_lock);
516 if(rtc_status & RTC_IS_OPEN)
517 goto out_busy;
519 rtc_status |= RTC_IS_OPEN;
521 rtc_irq_data = 0;
522 spin_unlock_irq (&rtc_lock);
523 return 0;
525 out_busy:
526 spin_unlock_irq (&rtc_lock);
527 return -EBUSY;
530 static int rtc_fasync (int fd, struct file *filp, int on)
533 return fasync_helper (fd, filp, on, &rtc_async_queue);
536 static int rtc_release(struct inode *inode, struct file *file)
538 #if RTC_IRQ
540 * Turn off all interrupts once the device is no longer
541 * in use, and clear the data.
544 unsigned char tmp;
546 spin_lock_irq(&rtc_lock);
547 tmp = CMOS_READ(RTC_CONTROL);
548 tmp &= ~RTC_PIE;
549 tmp &= ~RTC_AIE;
550 tmp &= ~RTC_UIE;
551 CMOS_WRITE(tmp, RTC_CONTROL);
552 CMOS_READ(RTC_INTR_FLAGS);
554 if (rtc_status & RTC_TIMER_ON) {
555 rtc_status &= ~RTC_TIMER_ON;
556 del_timer(&rtc_irq_timer);
558 spin_unlock_irq(&rtc_lock);
560 if (file->f_flags & FASYNC) {
561 rtc_fasync (-1, file, 0);
563 #endif
565 spin_lock_irq (&rtc_lock);
566 rtc_irq_data = 0;
567 spin_unlock_irq (&rtc_lock);
569 /* No need for locking -- nobody else can do anything until this rmw is
570 * committed, and no timer is running. */
571 rtc_status &= ~RTC_IS_OPEN;
572 return 0;
575 #if RTC_IRQ
576 /* Called without the kernel lock - fine */
577 static unsigned int rtc_poll(struct file *file, poll_table *wait)
579 unsigned long l;
581 poll_wait(file, &rtc_wait, wait);
583 spin_lock_irq (&rtc_lock);
584 l = rtc_irq_data;
585 spin_unlock_irq (&rtc_lock);
587 if (l != 0)
588 return POLLIN | POLLRDNORM;
589 return 0;
591 #endif
594 * The various file operations we support.
597 static struct file_operations rtc_fops = {
598 owner: THIS_MODULE,
599 llseek: rtc_llseek,
600 read: rtc_read,
601 #if RTC_IRQ
602 poll: rtc_poll,
603 #endif
604 ioctl: rtc_ioctl,
605 open: rtc_open,
606 release: rtc_release,
607 fasync: rtc_fasync,
610 static struct miscdevice rtc_dev=
612 RTC_MINOR,
613 "rtc",
614 &rtc_fops
617 static int __init rtc_init(void)
619 #if defined(__alpha__) || defined(__mips__)
620 unsigned int year, ctrl;
621 unsigned long uip_watchdog;
622 char *guess = NULL;
623 #endif
624 #ifdef __sparc__
625 struct linux_ebus *ebus;
626 struct linux_ebus_device *edev;
627 #endif
629 #ifdef __sparc__
630 for_each_ebus(ebus) {
631 for_each_ebusdev(edev, ebus) {
632 if(strcmp(edev->prom_name, "rtc") == 0) {
633 goto found;
637 printk(KERN_ERR "rtc_init: no PC rtc found\n");
638 return -EIO;
640 found:
641 rtc_port = edev->resource[0].start;
642 rtc_irq = edev->irqs[0];
644 * XXX Interrupt pin #7 in Espresso is shared between RTC and
645 * PCI Slot 2 INTA# (and some INTx# in Slot 1). SA_INTERRUPT here
646 * is asking for trouble with add-on boards. Change to SA_SHIRQ.
648 if(request_irq(rtc_irq, rtc_interrupt, SA_INTERRUPT, "rtc", (void *)&rtc_port)) {
650 * Standard way for sparc to print irq's is to use
651 * __irq_itoa(). I think for EBus it's ok to use %d.
653 printk(KERN_ERR "rtc: cannot register IRQ %d\n", rtc_irq);
654 return -EIO;
656 #else
657 if (check_region (RTC_PORT (0), RTC_IO_EXTENT))
659 printk(KERN_ERR "rtc: I/O port %d is not free.\n", RTC_PORT (0));
660 return -EIO;
663 #if RTC_IRQ
664 if(request_irq(RTC_IRQ, rtc_interrupt, SA_INTERRUPT, "rtc", NULL))
666 /* Yeah right, seeing as irq 8 doesn't even hit the bus. */
667 printk(KERN_ERR "rtc: IRQ %d is not free.\n", RTC_IRQ);
668 return -EIO;
670 #endif
672 request_region(RTC_PORT(0), RTC_IO_EXTENT, "rtc");
673 #endif /* __sparc__ vs. others */
675 misc_register(&rtc_dev);
676 create_proc_read_entry ("driver/rtc", 0, 0, rtc_read_proc, NULL);
678 #if defined(__alpha__) || defined(__mips__)
679 rtc_freq = HZ;
681 /* Each operating system on an Alpha uses its own epoch.
682 Let's try to guess which one we are using now. */
684 uip_watchdog = jiffies;
685 if (rtc_is_updating() != 0)
686 while (jiffies - uip_watchdog < 2*HZ/100)
687 barrier();
689 spin_lock_irq(&rtc_lock);
690 year = CMOS_READ(RTC_YEAR);
691 ctrl = CMOS_READ(RTC_CONTROL);
692 spin_unlock_irq(&rtc_lock);
694 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
695 BCD_TO_BIN(year); /* This should never happen... */
697 if (year >= 20 && year < 48) {
698 epoch = 1980;
699 guess = "ARC console";
700 } else if (year >= 48 && year < 70) {
701 epoch = 1952;
702 guess = "Digital UNIX";
703 } else if (year >= 70 && year < 100) {
704 epoch = 1928;
705 guess = "Digital DECstation";
707 if (guess)
708 printk(KERN_INFO "rtc: %s epoch (%lu) detected\n", guess, epoch);
709 #endif
710 #if RTC_IRQ
711 init_timer(&rtc_irq_timer);
712 rtc_irq_timer.function = rtc_dropped_irq;
713 spin_lock_irq(&rtc_lock);
714 /* Initialize periodic freq. to CMOS reset default, which is 1024Hz */
715 CMOS_WRITE(((CMOS_READ(RTC_FREQ_SELECT) & 0xF0) | 0x06), RTC_FREQ_SELECT);
716 spin_unlock_irq(&rtc_lock);
717 rtc_freq = 1024;
718 #endif
720 printk(KERN_INFO "Real Time Clock Driver v" RTC_VERSION "\n");
722 return 0;
725 static void __exit rtc_exit (void)
727 remove_proc_entry ("driver/rtc", NULL);
728 misc_deregister(&rtc_dev);
730 #ifdef __sparc__
731 free_irq (rtc_irq, &rtc_port);
732 #else
733 release_region (RTC_PORT (0), RTC_IO_EXTENT);
734 #if RTC_IRQ
735 free_irq (RTC_IRQ, NULL);
736 #endif
737 #endif /* __sparc__ */
740 module_init(rtc_init);
741 module_exit(rtc_exit);
742 EXPORT_NO_SYMBOLS;
744 #if RTC_IRQ
746 * At IRQ rates >= 4096Hz, an interrupt may get lost altogether.
747 * (usually during an IDE disk interrupt, with IRQ unmasking off)
748 * Since the interrupt handler doesn't get called, the IRQ status
749 * byte doesn't get read, and the RTC stops generating interrupts.
750 * A timer is set, and will call this function if/when that happens.
751 * To get it out of this stalled state, we just read the status.
752 * At least a jiffy of interrupts (rtc_freq/HZ) will have been lost.
753 * (You *really* shouldn't be trying to use a non-realtime system
754 * for something that requires a steady > 1KHz signal anyways.)
757 static void rtc_dropped_irq(unsigned long data)
759 unsigned long freq;
761 spin_lock_irq (&rtc_lock);
763 /* Just in case someone disabled the timer from behind our back... */
764 if (rtc_status & RTC_TIMER_ON)
765 mod_timer(&rtc_irq_timer, jiffies + HZ/rtc_freq + 2*HZ/100);
767 rtc_irq_data += ((rtc_freq/HZ)<<8);
768 rtc_irq_data &= ~0xff;
769 rtc_irq_data |= (CMOS_READ(RTC_INTR_FLAGS) & 0xF0); /* restart */
771 freq = rtc_freq;
773 spin_unlock_irq(&rtc_lock);
775 printk(KERN_WARNING "rtc: lost some interrupts at %ldHz.\n", freq);
777 /* Now we have new data */
778 wake_up_interruptible(&rtc_wait);
780 kill_fasync (&rtc_async_queue, SIGIO, POLL_IN);
782 #endif
785 * Info exported via "/proc/driver/rtc".
788 static int rtc_proc_output (char *buf)
790 #define YN(bit) ((ctrl & bit) ? "yes" : "no")
791 #define NY(bit) ((ctrl & bit) ? "no" : "yes")
792 char *p;
793 struct rtc_time tm;
794 unsigned char batt, ctrl;
795 unsigned long freq;
797 spin_lock_irq(&rtc_lock);
798 batt = CMOS_READ(RTC_VALID) & RTC_VRT;
799 ctrl = CMOS_READ(RTC_CONTROL);
800 freq = rtc_freq;
801 spin_unlock_irq(&rtc_lock);
803 p = buf;
805 get_rtc_time(&tm);
808 * There is no way to tell if the luser has the RTC set for local
809 * time or for Universal Standard Time (GMT). Probably local though.
811 p += sprintf(p,
812 "rtc_time\t: %02d:%02d:%02d\n"
813 "rtc_date\t: %04d-%02d-%02d\n"
814 "rtc_epoch\t: %04lu\n",
815 tm.tm_hour, tm.tm_min, tm.tm_sec,
816 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, epoch);
818 get_rtc_alm_time(&tm);
821 * We implicitly assume 24hr mode here. Alarm values >= 0xc0 will
822 * match any value for that particular field. Values that are
823 * greater than a valid time, but less than 0xc0 shouldn't appear.
825 p += sprintf(p, "alarm\t\t: ");
826 if (tm.tm_hour <= 24)
827 p += sprintf(p, "%02d:", tm.tm_hour);
828 else
829 p += sprintf(p, "**:");
831 if (tm.tm_min <= 59)
832 p += sprintf(p, "%02d:", tm.tm_min);
833 else
834 p += sprintf(p, "**:");
836 if (tm.tm_sec <= 59)
837 p += sprintf(p, "%02d\n", tm.tm_sec);
838 else
839 p += sprintf(p, "**\n");
841 p += sprintf(p,
842 "DST_enable\t: %s\n"
843 "BCD\t\t: %s\n"
844 "24hr\t\t: %s\n"
845 "square_wave\t: %s\n"
846 "alarm_IRQ\t: %s\n"
847 "update_IRQ\t: %s\n"
848 "periodic_IRQ\t: %s\n"
849 "periodic_freq\t: %ld\n"
850 "batt_status\t: %s\n",
851 YN(RTC_DST_EN),
852 NY(RTC_DM_BINARY),
853 YN(RTC_24H),
854 YN(RTC_SQWE),
855 YN(RTC_AIE),
856 YN(RTC_UIE),
857 YN(RTC_PIE),
858 freq,
859 batt ? "okay" : "dead");
861 return p - buf;
862 #undef YN
863 #undef NY
866 static int rtc_read_proc(char *page, char **start, off_t off,
867 int count, int *eof, void *data)
869 int len = rtc_proc_output (page);
870 if (len <= off+count) *eof = 1;
871 *start = page + off;
872 len -= off;
873 if (len>count) len = count;
874 if (len<0) len = 0;
875 return len;
879 * Returns true if a clock update is in progress
881 /* FIXME shouldn't this be above rtc_init to make it fully inlined? */
882 static inline unsigned char rtc_is_updating(void)
884 unsigned char uip;
886 spin_lock_irq(&rtc_lock);
887 uip = (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP);
888 spin_unlock_irq(&rtc_lock);
889 return uip;
892 static void get_rtc_time(struct rtc_time *rtc_tm)
894 unsigned long uip_watchdog = jiffies;
895 unsigned char ctrl;
898 * read RTC once any update in progress is done. The update
899 * can take just over 2ms. We wait 10 to 20ms. There is no need to
900 * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
901 * If you need to know *exactly* when a second has started, enable
902 * periodic update complete interrupts, (via ioctl) and then
903 * immediately read /dev/rtc which will block until you get the IRQ.
904 * Once the read clears, read the RTC time (again via ioctl). Easy.
907 if (rtc_is_updating() != 0)
908 while (jiffies - uip_watchdog < 2*HZ/100)
909 barrier();
912 * Only the values that we read from the RTC are set. We leave
913 * tm_wday, tm_yday and tm_isdst untouched. Even though the
914 * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
915 * by the RTC when initially set to a non-zero value.
917 spin_lock_irq(&rtc_lock);
918 rtc_tm->tm_sec = CMOS_READ(RTC_SECONDS);
919 rtc_tm->tm_min = CMOS_READ(RTC_MINUTES);
920 rtc_tm->tm_hour = CMOS_READ(RTC_HOURS);
921 rtc_tm->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH);
922 rtc_tm->tm_mon = CMOS_READ(RTC_MONTH);
923 rtc_tm->tm_year = CMOS_READ(RTC_YEAR);
924 ctrl = CMOS_READ(RTC_CONTROL);
925 spin_unlock_irq(&rtc_lock);
927 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
929 BCD_TO_BIN(rtc_tm->tm_sec);
930 BCD_TO_BIN(rtc_tm->tm_min);
931 BCD_TO_BIN(rtc_tm->tm_hour);
932 BCD_TO_BIN(rtc_tm->tm_mday);
933 BCD_TO_BIN(rtc_tm->tm_mon);
934 BCD_TO_BIN(rtc_tm->tm_year);
938 * Account for differences between how the RTC uses the values
939 * and how they are defined in a struct rtc_time;
941 if ((rtc_tm->tm_year += (epoch - 1900)) <= 69)
942 rtc_tm->tm_year += 100;
944 rtc_tm->tm_mon--;
947 static void get_rtc_alm_time(struct rtc_time *alm_tm)
949 unsigned char ctrl;
952 * Only the values that we read from the RTC are set. That
953 * means only tm_hour, tm_min, and tm_sec.
955 spin_lock_irq(&rtc_lock);
956 alm_tm->tm_sec = CMOS_READ(RTC_SECONDS_ALARM);
957 alm_tm->tm_min = CMOS_READ(RTC_MINUTES_ALARM);
958 alm_tm->tm_hour = CMOS_READ(RTC_HOURS_ALARM);
959 ctrl = CMOS_READ(RTC_CONTROL);
960 spin_unlock_irq(&rtc_lock);
962 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
964 BCD_TO_BIN(alm_tm->tm_sec);
965 BCD_TO_BIN(alm_tm->tm_min);
966 BCD_TO_BIN(alm_tm->tm_hour);
970 #if RTC_IRQ
972 * Used to disable/enable interrupts for any one of UIE, AIE, PIE.
973 * Rumour has it that if you frob the interrupt enable/disable
974 * bits in RTC_CONTROL, you should read RTC_INTR_FLAGS, to
975 * ensure you actually start getting interrupts. Probably for
976 * compatibility with older/broken chipset RTC implementations.
977 * We also clear out any old irq data after an ioctl() that
978 * meddles with the interrupt enable/disable bits.
981 static void mask_rtc_irq_bit(unsigned char bit)
983 unsigned char val;
985 spin_lock_irq(&rtc_lock);
986 val = CMOS_READ(RTC_CONTROL);
987 val &= ~bit;
988 CMOS_WRITE(val, RTC_CONTROL);
989 CMOS_READ(RTC_INTR_FLAGS);
991 rtc_irq_data = 0;
992 spin_unlock_irq(&rtc_lock);
995 static void set_rtc_irq_bit(unsigned char bit)
997 unsigned char val;
999 spin_lock_irq(&rtc_lock);
1000 val = CMOS_READ(RTC_CONTROL);
1001 val |= bit;
1002 CMOS_WRITE(val, RTC_CONTROL);
1003 CMOS_READ(RTC_INTR_FLAGS);
1005 rtc_irq_data = 0;
1006 spin_unlock_irq(&rtc_lock);
1008 #endif