Portability cleanup as required by Linus.
[linux-2.6/linux-mips.git] / drivers / char / rtc.c
bloba49e1c48e7ba202c4f168ad47c4a2c19d1bbe013
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_IO_EXTENT 0x10 /* Only really two ports, but... */
49 * Note that *all* calls to CMOS_READ and CMOS_WRITE are done with
50 * interrupts disabled. Due to the index-port/data-port (0x70/0x71)
51 * design of the RTC, we don't want two different things trying to
52 * get to it at once. (e.g. the periodic 11 min sync from time.c vs.
53 * this driver.)
56 #include <linux/module.h>
57 #include <linux/kernel.h>
58 #include <linux/types.h>
59 #include <linux/miscdevice.h>
60 #include <linux/ioport.h>
61 #include <linux/fcntl.h>
62 #include <linux/mc146818rtc.h>
63 #include <linux/init.h>
64 #include <linux/poll.h>
65 #include <linux/proc_fs.h>
66 #include <linux/spinlock.h>
68 #include <asm/io.h>
69 #include <asm/uaccess.h>
70 #include <asm/system.h>
72 #ifdef __sparc__
73 #include <asm/ebus.h>
75 static unsigned long rtc_port;
76 static int rtc_irq;
77 #endif
80 * We sponge a minor off of the misc major. No need slurping
81 * up another valuable major dev number for this. If you add
82 * an ioctl, make sure you don't conflict with SPARC's RTC
83 * ioctls.
86 static struct fasync_struct *rtc_async_queue;
88 static DECLARE_WAIT_QUEUE_HEAD(rtc_wait);
90 extern spinlock_t rtc_lock;
92 static struct timer_list rtc_irq_timer;
94 static loff_t rtc_llseek(struct file *file, loff_t offset, int origin);
96 static ssize_t rtc_read(struct file *file, char *buf,
97 size_t count, loff_t *ppos);
99 static int rtc_ioctl(struct inode *inode, struct file *file,
100 unsigned int cmd, unsigned long arg);
102 #if RTC_IRQ
103 static unsigned int rtc_poll(struct file *file, poll_table *wait);
104 #endif
106 static void get_rtc_time (struct rtc_time *rtc_tm);
107 static void get_rtc_alm_time (struct rtc_time *alm_tm);
108 #if RTC_IRQ
109 static void rtc_dropped_irq(unsigned long data);
111 static void set_rtc_irq_bit(unsigned char bit);
112 static void mask_rtc_irq_bit(unsigned char bit);
113 #endif
115 static inline unsigned char rtc_is_updating(void);
117 static int rtc_read_proc(char *page, char **start, off_t off,
118 int count, int *eof, void *data);
121 * Bits in rtc_status. (6 bits of room for future expansion)
124 #define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
125 #define RTC_TIMER_ON 0x02 /* missed irq timer active */
128 * rtc_status is never changed by rtc_interrupt, and ioctl/open/close is
129 * protected by the big kernel lock. However, ioctl can still disable the timer
130 * in rtc_status and then with del_timer after the interrupt has read
131 * rtc_status but before mod_timer is called, which would then reenable the
132 * timer (but you would need to have an awful timing before you'd trip on it)
134 static unsigned long rtc_status = 0; /* bitmapped status byte. */
135 static unsigned long rtc_freq = 0; /* Current periodic IRQ rate */
136 static unsigned long rtc_irq_data = 0; /* our output to the world */
139 * If this driver ever becomes modularised, it will be really nice
140 * to make the epoch retain its value across module reload...
143 static unsigned long epoch = 1900; /* year corresponding to 0x00 */
145 static const unsigned char days_in_mo[] =
146 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
148 #if RTC_IRQ
150 * A very tiny interrupt handler. It runs with SA_INTERRUPT set,
151 * but there is possibility of conflicting with the set_rtc_mmss()
152 * call (the rtc irq and the timer irq can easily run at the same
153 * time in two different CPUs). So we need to serializes
154 * accesses to the chip with the rtc_lock spinlock that each
155 * architecture should implement in the timer code.
156 * (See ./arch/XXXX/kernel/time.c for the set_rtc_mmss() function.)
159 static void rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
162 * Can be an alarm interrupt, update complete interrupt,
163 * or a periodic interrupt. We store the status in the
164 * low byte and the number of interrupts received since
165 * the last read in the remainder of rtc_irq_data.
168 spin_lock (&rtc_lock);
169 rtc_irq_data += 0x100;
170 rtc_irq_data &= ~0xff;
171 rtc_irq_data |= (CMOS_READ(RTC_INTR_FLAGS) & 0xF0);
173 if (rtc_status & RTC_TIMER_ON)
174 mod_timer(&rtc_irq_timer, jiffies + HZ/rtc_freq + 2*HZ/100);
176 spin_unlock (&rtc_lock);
178 /* Now do the rest of the actions */
179 wake_up_interruptible(&rtc_wait);
181 kill_fasync (&rtc_async_queue, SIGIO, POLL_IN);
183 #endif
186 * Now all the various file operations that we export.
189 static loff_t rtc_llseek(struct file *file, loff_t offset, int origin)
191 return -ESPIPE;
194 static ssize_t rtc_read(struct file *file, char *buf,
195 size_t count, loff_t *ppos)
197 #if !RTC_IRQ
198 return -EIO;
199 #else
200 DECLARE_WAITQUEUE(wait, current);
201 unsigned long data;
202 ssize_t retval;
204 if (count < sizeof(unsigned long))
205 return -EINVAL;
207 add_wait_queue(&rtc_wait, &wait);
209 current->state = TASK_INTERRUPTIBLE;
211 do {
212 /* First make it right. Then make it fast. Putting this whole
213 * block within the parentheses of a while would be too
214 * confusing. And no, xchg() is not the answer. */
215 spin_lock_irq (&rtc_lock);
216 data = rtc_irq_data;
217 rtc_irq_data = 0;
218 spin_unlock_irq (&rtc_lock);
220 if (data != 0)
221 break;
223 if (file->f_flags & O_NONBLOCK) {
224 retval = -EAGAIN;
225 goto out;
227 if (signal_pending(current)) {
228 retval = -ERESTARTSYS;
229 goto out;
231 schedule();
232 } while (1);
234 retval = put_user(data, (unsigned long *)buf);
235 if (!retval)
236 retval = sizeof(unsigned long);
237 out:
238 current->state = TASK_RUNNING;
239 remove_wait_queue(&rtc_wait, &wait);
241 return retval;
242 #endif
245 static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
246 unsigned long arg)
248 struct rtc_time wtime;
250 switch (cmd) {
251 #if RTC_IRQ
252 case RTC_AIE_OFF: /* Mask alarm int. enab. bit */
254 mask_rtc_irq_bit(RTC_AIE);
255 return 0;
257 case RTC_AIE_ON: /* Allow alarm interrupts. */
259 set_rtc_irq_bit(RTC_AIE);
260 return 0;
262 case RTC_PIE_OFF: /* Mask periodic int. enab. bit */
264 mask_rtc_irq_bit(RTC_PIE);
265 if (rtc_status & RTC_TIMER_ON) {
266 spin_lock_irq (&rtc_lock);
267 rtc_status &= ~RTC_TIMER_ON;
268 del_timer(&rtc_irq_timer);
269 spin_unlock_irq (&rtc_lock);
271 return 0;
273 case RTC_PIE_ON: /* Allow periodic ints */
277 * We don't really want Joe User enabling more
278 * than 64Hz of interrupts on a multi-user machine.
280 if ((rtc_freq > 64) && (!capable(CAP_SYS_RESOURCE)))
281 return -EACCES;
283 if (!(rtc_status & RTC_TIMER_ON)) {
284 spin_lock_irq (&rtc_lock);
285 rtc_irq_timer.expires = jiffies + HZ/rtc_freq + 2*HZ/100;
286 add_timer(&rtc_irq_timer);
287 rtc_status |= RTC_TIMER_ON;
288 spin_unlock_irq (&rtc_lock);
290 set_rtc_irq_bit(RTC_PIE);
291 return 0;
293 case RTC_UIE_OFF: /* Mask ints from RTC updates. */
295 mask_rtc_irq_bit(RTC_UIE);
296 return 0;
298 case RTC_UIE_ON: /* Allow ints for RTC updates. */
300 set_rtc_irq_bit(RTC_UIE);
301 return 0;
303 #endif
304 case RTC_ALM_READ: /* Read the present alarm time */
307 * This returns a struct rtc_time. Reading >= 0xc0
308 * means "don't care" or "match all". Only the tm_hour,
309 * tm_min, and tm_sec values are filled in.
312 get_rtc_alm_time(&wtime);
313 break;
315 case RTC_ALM_SET: /* Store a time into the alarm */
318 * This expects a struct rtc_time. Writing 0xff means
319 * "don't care" or "match all". Only the tm_hour,
320 * tm_min and tm_sec are used.
322 unsigned char hrs, min, sec;
323 struct rtc_time alm_tm;
325 if (copy_from_user(&alm_tm, (struct rtc_time*)arg,
326 sizeof(struct rtc_time)))
327 return -EFAULT;
329 hrs = alm_tm.tm_hour;
330 min = alm_tm.tm_min;
331 sec = alm_tm.tm_sec;
333 if (hrs >= 24)
334 hrs = 0xff;
336 if (min >= 60)
337 min = 0xff;
339 if (sec >= 60)
340 sec = 0xff;
342 spin_lock_irq(&rtc_lock);
343 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) ||
344 RTC_ALWAYS_BCD)
346 BIN_TO_BCD(sec);
347 BIN_TO_BCD(min);
348 BIN_TO_BCD(hrs);
350 CMOS_WRITE(hrs, RTC_HOURS_ALARM);
351 CMOS_WRITE(min, RTC_MINUTES_ALARM);
352 CMOS_WRITE(sec, RTC_SECONDS_ALARM);
353 spin_unlock_irq(&rtc_lock);
355 return 0;
357 case RTC_RD_TIME: /* Read the time/date from RTC */
359 get_rtc_time(&wtime);
360 break;
362 case RTC_SET_TIME: /* Set the RTC */
364 struct rtc_time rtc_tm;
365 unsigned char mon, day, hrs, min, sec, leap_yr;
366 unsigned char save_control, save_freq_select;
367 unsigned int yrs;
369 if (!capable(CAP_SYS_TIME))
370 return -EACCES;
372 if (copy_from_user(&rtc_tm, (struct rtc_time*)arg,
373 sizeof(struct rtc_time)))
374 return -EFAULT;
376 yrs = rtc_tm.tm_year + 1900;
377 mon = rtc_tm.tm_mon + 1; /* tm_mon starts at zero */
378 day = rtc_tm.tm_mday;
379 hrs = rtc_tm.tm_hour;
380 min = rtc_tm.tm_min;
381 sec = rtc_tm.tm_sec;
383 if (yrs < 1970)
384 return -EINVAL;
386 leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
388 if ((mon > 12) || (day == 0))
389 return -EINVAL;
391 if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
392 return -EINVAL;
394 if ((hrs >= 24) || (min >= 60) || (sec >= 60))
395 return -EINVAL;
397 if ((yrs -= epoch) > 255) /* They are unsigned */
398 return -EINVAL;
400 spin_lock_irq(&rtc_lock);
401 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY)
402 || RTC_ALWAYS_BCD) {
403 if (yrs > 169) {
404 spin_unlock_irq(&rtc_lock);
405 return -EINVAL;
407 if (yrs >= 100)
408 yrs -= 100;
410 BIN_TO_BCD(sec);
411 BIN_TO_BCD(min);
412 BIN_TO_BCD(hrs);
413 BIN_TO_BCD(day);
414 BIN_TO_BCD(mon);
415 BIN_TO_BCD(yrs);
418 save_control = CMOS_READ(RTC_CONTROL);
419 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
420 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
421 CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
423 CMOS_WRITE(yrs, RTC_YEAR);
424 CMOS_WRITE(mon, RTC_MONTH);
425 CMOS_WRITE(day, RTC_DAY_OF_MONTH);
426 CMOS_WRITE(hrs, RTC_HOURS);
427 CMOS_WRITE(min, RTC_MINUTES);
428 CMOS_WRITE(sec, RTC_SECONDS);
430 CMOS_WRITE(save_control, RTC_CONTROL);
431 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
433 spin_unlock_irq(&rtc_lock);
434 return 0;
436 #if RTC_IRQ
437 case RTC_IRQP_READ: /* Read the periodic IRQ rate. */
439 return put_user(rtc_freq, (unsigned long *)arg);
441 case RTC_IRQP_SET: /* Set periodic IRQ rate. */
443 int tmp = 0;
444 unsigned char val;
447 * The max we can do is 8192Hz.
449 if ((arg < 2) || (arg > 8192))
450 return -EINVAL;
452 * We don't really want Joe User generating more
453 * than 64Hz of interrupts on a multi-user machine.
455 if ((arg > 64) && (!capable(CAP_SYS_RESOURCE)))
456 return -EACCES;
458 while (arg > (1<<tmp))
459 tmp++;
462 * Check that the input was really a power of 2.
464 if (arg != (1<<tmp))
465 return -EINVAL;
467 spin_lock_irq(&rtc_lock);
468 rtc_freq = arg;
470 val = CMOS_READ(RTC_FREQ_SELECT) & 0xf0;
471 val |= (16 - tmp);
472 CMOS_WRITE(val, RTC_FREQ_SELECT);
473 spin_unlock_irq(&rtc_lock);
474 return 0;
476 #elif !defined(CONFIG_DECSTATION)
477 case RTC_EPOCH_READ: /* Read the epoch. */
479 return put_user (epoch, (unsigned long *)arg);
481 case RTC_EPOCH_SET: /* Set the epoch. */
484 * There were no RTC clocks before 1900.
486 if (arg < 1900)
487 return -EINVAL;
489 if (!capable(CAP_SYS_TIME))
490 return -EACCES;
492 epoch = arg;
493 return 0;
495 #endif
496 default:
497 return -EINVAL;
499 return copy_to_user((void *)arg, &wtime, sizeof wtime) ? -EFAULT : 0;
503 * We enforce only one user at a time here with the open/close.
504 * Also clear the previous interrupt data on an open, and clean
505 * up things on a close.
508 /* We use rtc_lock to protect against concurrent opens. So the BKL is not
509 * needed here. Or anywhere else in this driver. */
510 static int rtc_open(struct inode *inode, struct file *file)
512 spin_lock_irq (&rtc_lock);
514 if(rtc_status & RTC_IS_OPEN)
515 goto out_busy;
517 rtc_status |= RTC_IS_OPEN;
519 rtc_irq_data = 0;
520 spin_unlock_irq (&rtc_lock);
521 return 0;
523 out_busy:
524 spin_unlock_irq (&rtc_lock);
525 return -EBUSY;
528 static int rtc_fasync (int fd, struct file *filp, int on)
531 return fasync_helper (fd, filp, on, &rtc_async_queue);
534 static int rtc_release(struct inode *inode, struct file *file)
536 #if RTC_IRQ
538 * Turn off all interrupts once the device is no longer
539 * in use, and clear the data.
542 unsigned char tmp;
544 spin_lock_irq(&rtc_lock);
545 tmp = CMOS_READ(RTC_CONTROL);
546 tmp &= ~RTC_PIE;
547 tmp &= ~RTC_AIE;
548 tmp &= ~RTC_UIE;
549 CMOS_WRITE(tmp, RTC_CONTROL);
550 CMOS_READ(RTC_INTR_FLAGS);
552 if (rtc_status & RTC_TIMER_ON) {
553 rtc_status &= ~RTC_TIMER_ON;
554 del_timer(&rtc_irq_timer);
556 spin_unlock_irq(&rtc_lock);
558 if (file->f_flags & FASYNC) {
559 rtc_fasync (-1, file, 0);
561 #endif
563 spin_lock_irq (&rtc_lock);
564 rtc_irq_data = 0;
565 spin_unlock_irq (&rtc_lock);
567 /* No need for locking -- nobody else can do anything until this rmw is
568 * committed, and no timer is running. */
569 rtc_status &= ~RTC_IS_OPEN;
570 return 0;
573 #if RTC_IRQ
574 /* Called without the kernel lock - fine */
575 static unsigned int rtc_poll(struct file *file, poll_table *wait)
577 unsigned long l;
579 poll_wait(file, &rtc_wait, wait);
581 spin_lock_irq (&rtc_lock);
582 l = rtc_irq_data;
583 spin_unlock_irq (&rtc_lock);
585 if (l != 0)
586 return POLLIN | POLLRDNORM;
587 return 0;
589 #endif
592 * The various file operations we support.
595 static struct file_operations rtc_fops = {
596 owner: THIS_MODULE,
597 llseek: rtc_llseek,
598 read: rtc_read,
599 #if RTC_IRQ
600 poll: rtc_poll,
601 #endif
602 ioctl: rtc_ioctl,
603 open: rtc_open,
604 release: rtc_release,
605 fasync: rtc_fasync,
608 static struct miscdevice rtc_dev=
610 RTC_MINOR,
611 "rtc",
612 &rtc_fops
615 static int __init rtc_init(void)
617 #if defined(__alpha__) || defined(__mips__)
618 unsigned int year, ctrl;
619 unsigned long uip_watchdog;
620 char *guess = NULL;
621 #endif
622 #ifdef __sparc__
623 struct linux_ebus *ebus;
624 struct linux_ebus_device *edev;
625 #endif
627 #ifdef __sparc__
628 for_each_ebus(ebus) {
629 for_each_ebusdev(edev, ebus) {
630 if(strcmp(edev->prom_name, "rtc") == 0) {
631 goto found;
635 printk("rtc_init: no PC rtc found\n");
636 return -EIO;
638 found:
639 rtc_port = edev->resource[0].start;
640 rtc_irq = edev->irqs[0];
642 * XXX Interrupt pin #7 in Espresso is shared between RTC and
643 * PCI Slot 2 INTA# (and some INTx# in Slot 1). SA_INTERRUPT here
644 * is asking for trouble with add-on boards. Change to SA_SHIRQ.
646 if(request_irq(rtc_irq, rtc_interrupt, SA_INTERRUPT, "rtc", (void *)&rtc_port)) {
648 * Standard way for sparc to print irq's is to use
649 * __irq_itoa(). I think for EBus it's ok to use %d.
651 printk("rtc: cannot register IRQ %d\n", rtc_irq);
652 return -EIO;
654 #else
655 if (check_region (RTC_PORT (0), RTC_IO_EXTENT))
657 printk(KERN_ERR "rtc: I/O port %d is not free.\n", RTC_PORT (0));
658 return -EIO;
661 #if RTC_IRQ
662 if(request_irq(RTC_IRQ, rtc_interrupt, SA_INTERRUPT, "rtc", NULL))
664 /* Yeah right, seeing as irq 8 doesn't even hit the bus. */
665 printk(KERN_ERR "rtc: IRQ %d is not free.\n", RTC_IRQ);
666 return -EIO;
668 #endif
670 request_region(RTC_PORT(0), RTC_IO_EXTENT, "rtc");
671 #endif /* __sparc__ vs. others */
673 misc_register(&rtc_dev);
674 create_proc_read_entry ("driver/rtc", 0, 0, rtc_read_proc, NULL);
676 #if defined(__alpha__) || defined(__mips__)
677 rtc_freq = HZ;
679 /* Each operating system on an Alpha uses its own epoch.
680 Let's try to guess which one we are using now. */
682 uip_watchdog = jiffies;
683 if (rtc_is_updating() != 0)
684 while (jiffies - uip_watchdog < 2*HZ/100)
685 barrier();
687 spin_lock_irq(&rtc_lock);
688 year = CMOS_READ(RTC_YEAR);
689 ctrl = CMOS_READ(RTC_CONTROL);
690 spin_unlock_irq(&rtc_lock);
692 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
693 BCD_TO_BIN(year); /* This should never happen... */
695 if (year > 20 && year < 48) {
696 epoch = 1980;
697 guess = "ARC console";
698 } else if (year >= 48 && year < 70) {
699 epoch = 1952;
700 guess = "Digital UNIX";
701 } else if (year >= 70 && year < 100) {
702 epoch = 1928;
703 guess = "Digital DECstation";
705 if (guess)
706 printk("rtc: %s epoch (%lu) detected\n", guess, epoch);
707 #endif
708 #if RTC_IRQ
709 init_timer(&rtc_irq_timer);
710 rtc_irq_timer.function = rtc_dropped_irq;
711 spin_lock_irq(&rtc_lock);
712 /* Initialize periodic freq. to CMOS reset default, which is 1024Hz */
713 CMOS_WRITE(((CMOS_READ(RTC_FREQ_SELECT) & 0xF0) | 0x06), RTC_FREQ_SELECT);
714 spin_unlock_irq(&rtc_lock);
715 rtc_freq = 1024;
716 #endif
718 printk(KERN_INFO "Real Time Clock Driver v" RTC_VERSION "\n");
720 return 0;
723 static void __exit rtc_exit (void)
725 /* interrupts and maybe timer disabled at this point by rtc_release */
726 /* FIXME: Maybe??? */
728 if (rtc_status & RTC_TIMER_ON) {
729 spin_lock_irq (&rtc_lock);
730 rtc_status &= ~RTC_TIMER_ON;
731 del_timer(&rtc_irq_timer);
732 spin_unlock_irq (&rtc_lock);
734 printk(KERN_WARNING "rtc_exit(), and timer still running.\n");
737 remove_proc_entry ("driver/rtc", NULL);
738 misc_deregister(&rtc_dev);
740 #ifdef __sparc__
741 free_irq (rtc_irq, &rtc_port);
742 #else
743 release_region (RTC_PORT (0), RTC_IO_EXTENT);
744 #if RTC_IRQ
745 free_irq (RTC_IRQ, NULL);
746 #endif
747 #endif /* __sparc__ */
750 module_init(rtc_init);
751 module_exit(rtc_exit);
752 EXPORT_NO_SYMBOLS;
754 #if RTC_IRQ
756 * At IRQ rates >= 4096Hz, an interrupt may get lost altogether.
757 * (usually during an IDE disk interrupt, with IRQ unmasking off)
758 * Since the interrupt handler doesn't get called, the IRQ status
759 * byte doesn't get read, and the RTC stops generating interrupts.
760 * A timer is set, and will call this function if/when that happens.
761 * To get it out of this stalled state, we just read the status.
762 * At least a jiffy of interrupts (rtc_freq/HZ) will have been lost.
763 * (You *really* shouldn't be trying to use a non-realtime system
764 * for something that requires a steady > 1KHz signal anyways.)
767 static void rtc_dropped_irq(unsigned long data)
769 unsigned long freq;
771 spin_lock_irq (&rtc_lock);
773 /* Just in case someone disabled the timer from behind our back... */
774 if (rtc_status & RTC_TIMER_ON)
775 mod_timer(&rtc_irq_timer, jiffies + HZ/rtc_freq + 2*HZ/100);
777 rtc_irq_data += ((rtc_freq/HZ)<<8);
778 rtc_irq_data &= ~0xff;
779 rtc_irq_data |= (CMOS_READ(RTC_INTR_FLAGS) & 0xF0); /* restart */
781 freq = rtc_freq;
783 spin_unlock_irq(&rtc_lock);
785 printk(KERN_INFO "rtc: lost some interrupts at %ldHz.\n", freq);
787 /* Now we have new data */
788 wake_up_interruptible(&rtc_wait);
790 kill_fasync (&rtc_async_queue, SIGIO, POLL_IN);
792 #endif
795 * Info exported via "/proc/driver/rtc".
798 static int rtc_proc_output (char *buf)
800 #define YN(bit) ((ctrl & bit) ? "yes" : "no")
801 #define NY(bit) ((ctrl & bit) ? "no" : "yes")
802 char *p;
803 struct rtc_time tm;
804 unsigned char batt, ctrl;
805 unsigned long freq;
807 spin_lock_irq(&rtc_lock);
808 batt = CMOS_READ(RTC_VALID) & RTC_VRT;
809 ctrl = CMOS_READ(RTC_CONTROL);
810 freq = rtc_freq;
811 spin_unlock_irq(&rtc_lock);
813 p = buf;
815 get_rtc_time(&tm);
818 * There is no way to tell if the luser has the RTC set for local
819 * time or for Universal Standard Time (GMT). Probably local though.
821 p += sprintf(p,
822 "rtc_time\t: %02d:%02d:%02d\n"
823 "rtc_date\t: %04d-%02d-%02d\n"
824 "rtc_epoch\t: %04lu\n",
825 tm.tm_hour, tm.tm_min, tm.tm_sec,
826 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, epoch);
828 get_rtc_alm_time(&tm);
831 * We implicitly assume 24hr mode here. Alarm values >= 0xc0 will
832 * match any value for that particular field. Values that are
833 * greater than a valid time, but less than 0xc0 shouldn't appear.
835 p += sprintf(p, "alarm\t\t: ");
836 if (tm.tm_hour <= 24)
837 p += sprintf(p, "%02d:", tm.tm_hour);
838 else
839 p += sprintf(p, "**:");
841 if (tm.tm_min <= 59)
842 p += sprintf(p, "%02d:", tm.tm_min);
843 else
844 p += sprintf(p, "**:");
846 if (tm.tm_sec <= 59)
847 p += sprintf(p, "%02d\n", tm.tm_sec);
848 else
849 p += sprintf(p, "**\n");
851 p += sprintf(p,
852 "DST_enable\t: %s\n"
853 "BCD\t\t: %s\n"
854 "24hr\t\t: %s\n"
855 "square_wave\t: %s\n"
856 "alarm_IRQ\t: %s\n"
857 "update_IRQ\t: %s\n"
858 "periodic_IRQ\t: %s\n"
859 "periodic_freq\t: %ld\n"
860 "batt_status\t: %s\n",
861 YN(RTC_DST_EN),
862 NY(RTC_DM_BINARY),
863 YN(RTC_24H),
864 YN(RTC_SQWE),
865 YN(RTC_AIE),
866 YN(RTC_UIE),
867 YN(RTC_PIE),
868 freq,
869 batt ? "okay" : "dead");
871 return p - buf;
872 #undef YN
873 #undef NY
876 static int rtc_read_proc(char *page, char **start, off_t off,
877 int count, int *eof, void *data)
879 int len = rtc_proc_output (page);
880 if (len <= off+count) *eof = 1;
881 *start = page + off;
882 len -= off;
883 if (len>count) len = count;
884 if (len<0) len = 0;
885 return len;
889 * Returns true if a clock update is in progress
891 /* FIXME shouldn't this be above rtc_init to make it fully inlined? */
892 static inline unsigned char rtc_is_updating(void)
894 unsigned char uip;
896 spin_lock_irq(&rtc_lock);
897 uip = (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP);
898 spin_unlock_irq(&rtc_lock);
899 return uip;
902 static void get_rtc_time(struct rtc_time *rtc_tm)
904 unsigned long uip_watchdog = jiffies;
905 unsigned char ctrl;
908 * read RTC once any update in progress is done. The update
909 * can take just over 2ms. We wait 10 to 20ms. There is no need to
910 * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
911 * If you need to know *exactly* when a second has started, enable
912 * periodic update complete interrupts, (via ioctl) and then
913 * immediately read /dev/rtc which will block until you get the IRQ.
914 * Once the read clears, read the RTC time (again via ioctl). Easy.
917 if (rtc_is_updating() != 0)
918 while (jiffies - uip_watchdog < 2*HZ/100)
919 barrier();
922 * Only the values that we read from the RTC are set. We leave
923 * tm_wday, tm_yday and tm_isdst untouched. Even though the
924 * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
925 * by the RTC when initially set to a non-zero value.
927 spin_lock_irq(&rtc_lock);
928 rtc_tm->tm_sec = CMOS_READ(RTC_SECONDS);
929 rtc_tm->tm_min = CMOS_READ(RTC_MINUTES);
930 rtc_tm->tm_hour = CMOS_READ(RTC_HOURS);
931 rtc_tm->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH);
932 rtc_tm->tm_mon = CMOS_READ(RTC_MONTH);
933 rtc_tm->tm_year = CMOS_READ(RTC_YEAR);
934 ctrl = CMOS_READ(RTC_CONTROL);
935 spin_unlock_irq(&rtc_lock);
937 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
939 BCD_TO_BIN(rtc_tm->tm_sec);
940 BCD_TO_BIN(rtc_tm->tm_min);
941 BCD_TO_BIN(rtc_tm->tm_hour);
942 BCD_TO_BIN(rtc_tm->tm_mday);
943 BCD_TO_BIN(rtc_tm->tm_mon);
944 BCD_TO_BIN(rtc_tm->tm_year);
948 * Account for differences between how the RTC uses the values
949 * and how they are defined in a struct rtc_time;
951 if ((rtc_tm->tm_year += (epoch - 1900)) <= 69)
952 rtc_tm->tm_year += 100;
954 rtc_tm->tm_mon--;
957 static void get_rtc_alm_time(struct rtc_time *alm_tm)
959 unsigned char ctrl;
962 * Only the values that we read from the RTC are set. That
963 * means only tm_hour, tm_min, and tm_sec.
965 spin_lock_irq(&rtc_lock);
966 alm_tm->tm_sec = CMOS_READ(RTC_SECONDS_ALARM);
967 alm_tm->tm_min = CMOS_READ(RTC_MINUTES_ALARM);
968 alm_tm->tm_hour = CMOS_READ(RTC_HOURS_ALARM);
969 ctrl = CMOS_READ(RTC_CONTROL);
970 spin_unlock_irq(&rtc_lock);
972 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
974 BCD_TO_BIN(alm_tm->tm_sec);
975 BCD_TO_BIN(alm_tm->tm_min);
976 BCD_TO_BIN(alm_tm->tm_hour);
980 #if RTC_IRQ
982 * Used to disable/enable interrupts for any one of UIE, AIE, PIE.
983 * Rumour has it that if you frob the interrupt enable/disable
984 * bits in RTC_CONTROL, you should read RTC_INTR_FLAGS, to
985 * ensure you actually start getting interrupts. Probably for
986 * compatibility with older/broken chipset RTC implementations.
987 * We also clear out any old irq data after an ioctl() that
988 * meddles with the interrupt enable/disable bits.
991 static void mask_rtc_irq_bit(unsigned char bit)
993 unsigned char val;
995 spin_lock_irq(&rtc_lock);
996 val = CMOS_READ(RTC_CONTROL);
997 val &= ~bit;
998 CMOS_WRITE(val, RTC_CONTROL);
999 CMOS_READ(RTC_INTR_FLAGS);
1001 rtc_irq_data = 0;
1002 spin_unlock_irq(&rtc_lock);
1005 static void set_rtc_irq_bit(unsigned char bit)
1007 unsigned char val;
1009 spin_lock_irq(&rtc_lock);
1010 val = CMOS_READ(RTC_CONTROL);
1011 val |= bit;
1012 CMOS_WRITE(val, RTC_CONTROL);
1013 CMOS_READ(RTC_INTR_FLAGS);
1015 rtc_irq_data = 0;
1016 spin_unlock_irq(&rtc_lock);
1018 #endif