[PATCH] Fix up 'linux-dvb' maintainers entry
[linux-2.6/history.git] / drivers / char / rtc.c
blobdfe289eaef61cc54f9860c31d717745e2483cfd0
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
43 * 1.10e Maciej W. Rozycki: Handle DECstation's year weirdness.
44 * 1.11 Takashi Iwai: Kernel access functions
45 * rtc_register/rtc_unregister/rtc_control
46 * 1.11a Daniele Bellucci: Audit create_proc_read_entry in rtc_init
47 * 1.12 Venkatesh Pallipadi: Hooks for emulating rtc on HPET base-timer
48 * CONFIG_HPET_EMULATE_RTC
52 #define RTC_VERSION "1.12"
54 #define RTC_IO_EXTENT 0x8
57 * Note that *all* calls to CMOS_READ and CMOS_WRITE are done with
58 * interrupts disabled. Due to the index-port/data-port (0x70/0x71)
59 * design of the RTC, we don't want two different things trying to
60 * get to it at once. (e.g. the periodic 11 min sync from time.c vs.
61 * this driver.)
64 #include <linux/config.h>
65 #include <linux/interrupt.h>
66 #include <linux/module.h>
67 #include <linux/kernel.h>
68 #include <linux/types.h>
69 #include <linux/miscdevice.h>
70 #include <linux/ioport.h>
71 #include <linux/fcntl.h>
72 #include <linux/mc146818rtc.h>
73 #include <linux/init.h>
74 #include <linux/poll.h>
75 #include <linux/proc_fs.h>
76 #include <linux/spinlock.h>
77 #include <linux/sysctl.h>
78 #include <linux/wait.h>
79 #include <linux/bcd.h>
81 #include <asm/current.h>
82 #include <asm/uaccess.h>
83 #include <asm/system.h>
85 #if defined(__i386__)
86 #include <asm/hpet.h>
87 #endif
89 #ifdef __sparc__
90 #include <linux/pci.h>
91 #include <asm/ebus.h>
92 #ifdef __sparc_v9__
93 #include <asm/isa.h>
94 #endif
96 static unsigned long rtc_port;
97 static int rtc_irq = PCI_IRQ_NONE;
98 #endif
100 #if RTC_IRQ
101 static int rtc_has_irq = 1;
102 #endif
104 #ifndef CONFIG_HPET_EMULATE_RTC
105 #define is_hpet_enabled() 0
106 #define hpet_set_alarm_time(hrs, min, sec) 0
107 #define hpet_set_periodic_freq(arg) 0
108 #define hpet_mask_rtc_irq_bit(arg) 0
109 #define hpet_set_rtc_irq_bit(arg) 0
110 #define hpet_rtc_timer_init() do { } while (0)
111 #define hpet_rtc_dropped_irq() 0
112 static inline irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs) {return 0;}
113 #endif
116 * We sponge a minor off of the misc major. No need slurping
117 * up another valuable major dev number for this. If you add
118 * an ioctl, make sure you don't conflict with SPARC's RTC
119 * ioctls.
122 static struct fasync_struct *rtc_async_queue;
124 static DECLARE_WAIT_QUEUE_HEAD(rtc_wait);
126 #if RTC_IRQ
127 static struct timer_list rtc_irq_timer;
128 #endif
130 static ssize_t rtc_read(struct file *file, char *buf,
131 size_t count, loff_t *ppos);
133 static int rtc_ioctl(struct inode *inode, struct file *file,
134 unsigned int cmd, unsigned long arg);
136 #if RTC_IRQ
137 static unsigned int rtc_poll(struct file *file, poll_table *wait);
138 #endif
140 static void get_rtc_alm_time (struct rtc_time *alm_tm);
141 #if RTC_IRQ
142 static void rtc_dropped_irq(unsigned long data);
144 static void set_rtc_irq_bit(unsigned char bit);
145 static void mask_rtc_irq_bit(unsigned char bit);
146 #endif
148 static inline unsigned char rtc_is_updating(void);
150 static int rtc_read_proc(char *page, char **start, off_t off,
151 int count, int *eof, void *data);
154 * Bits in rtc_status. (6 bits of room for future expansion)
157 #define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
158 #define RTC_TIMER_ON 0x02 /* missed irq timer active */
161 * rtc_status is never changed by rtc_interrupt, and ioctl/open/close is
162 * protected by the big kernel lock. However, ioctl can still disable the timer
163 * in rtc_status and then with del_timer after the interrupt has read
164 * rtc_status but before mod_timer is called, which would then reenable the
165 * timer (but you would need to have an awful timing before you'd trip on it)
167 static unsigned long rtc_status = 0; /* bitmapped status byte. */
168 static unsigned long rtc_freq = 0; /* Current periodic IRQ rate */
169 static unsigned long rtc_irq_data = 0; /* our output to the world */
170 static unsigned long rtc_max_user_freq = 64; /* > this, need CAP_SYS_RESOURCE */
172 #if RTC_IRQ
174 * rtc_task_lock nests inside rtc_lock.
176 static spinlock_t rtc_task_lock = SPIN_LOCK_UNLOCKED;
177 static rtc_task_t *rtc_callback = NULL;
178 #endif
181 * If this driver ever becomes modularised, it will be really nice
182 * to make the epoch retain its value across module reload...
185 static unsigned long epoch = 1900; /* year corresponding to 0x00 */
187 static const unsigned char days_in_mo[] =
188 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
190 #if RTC_IRQ
192 * A very tiny interrupt handler. It runs with SA_INTERRUPT set,
193 * but there is possibility of conflicting with the set_rtc_mmss()
194 * call (the rtc irq and the timer irq can easily run at the same
195 * time in two different CPUs). So we need to serialize
196 * accesses to the chip with the rtc_lock spinlock that each
197 * architecture should implement in the timer code.
198 * (See ./arch/XXXX/kernel/time.c for the set_rtc_mmss() function.)
201 irqreturn_t rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
204 * Can be an alarm interrupt, update complete interrupt,
205 * or a periodic interrupt. We store the status in the
206 * low byte and the number of interrupts received since
207 * the last read in the remainder of rtc_irq_data.
210 spin_lock (&rtc_lock);
211 rtc_irq_data += 0x100;
212 rtc_irq_data &= ~0xff;
213 if (is_hpet_enabled()) {
215 * In this case it is HPET RTC interrupt handler
216 * calling us, with the interrupt information
217 * passed as arg1, instead of irq.
219 rtc_irq_data |= (unsigned long)irq & 0xF0;
220 } else {
221 rtc_irq_data |= (CMOS_READ(RTC_INTR_FLAGS) & 0xF0);
224 if (rtc_status & RTC_TIMER_ON)
225 mod_timer(&rtc_irq_timer, jiffies + HZ/rtc_freq + 2*HZ/100);
227 spin_unlock (&rtc_lock);
229 /* Now do the rest of the actions */
230 spin_lock(&rtc_task_lock);
231 if (rtc_callback)
232 rtc_callback->func(rtc_callback->private_data);
233 spin_unlock(&rtc_task_lock);
234 wake_up_interruptible(&rtc_wait);
236 kill_fasync (&rtc_async_queue, SIGIO, POLL_IN);
238 return IRQ_HANDLED;
240 #endif
243 * sysctl-tuning infrastructure.
245 static ctl_table rtc_table[] = {
247 .ctl_name = 1,
248 .procname = "max-user-freq",
249 .data = &rtc_max_user_freq,
250 .maxlen = sizeof(int),
251 .mode = 0644,
252 .proc_handler = &proc_dointvec,
254 { .ctl_name = 0 }
257 static ctl_table rtc_root[] = {
259 .ctl_name = 1,
260 .procname = "rtc",
261 .maxlen = 0,
262 .mode = 0555,
263 .child = rtc_table,
265 { .ctl_name = 0 }
268 static ctl_table dev_root[] = {
270 .ctl_name = CTL_DEV,
271 .procname = "dev",
272 .maxlen = 0,
273 .mode = 0555,
274 .child = rtc_root,
276 { .ctl_name = 0 }
279 static struct ctl_table_header *sysctl_header;
281 static int __init init_sysctl(void)
283 sysctl_header = register_sysctl_table(dev_root, 0);
284 return 0;
287 static void __exit cleanup_sysctl(void)
289 unregister_sysctl_table(sysctl_header);
293 * Now all the various file operations that we export.
296 static ssize_t rtc_read(struct file *file, char *buf,
297 size_t count, loff_t *ppos)
299 #if !RTC_IRQ
300 return -EIO;
301 #else
302 DECLARE_WAITQUEUE(wait, current);
303 unsigned long data;
304 ssize_t retval;
306 if (rtc_has_irq == 0)
307 return -EIO;
309 if (count < sizeof(unsigned))
310 return -EINVAL;
312 add_wait_queue(&rtc_wait, &wait);
314 do {
315 /* First make it right. Then make it fast. Putting this whole
316 * block within the parentheses of a while would be too
317 * confusing. And no, xchg() is not the answer. */
319 __set_current_state(TASK_INTERRUPTIBLE);
321 spin_lock_irq (&rtc_lock);
322 data = rtc_irq_data;
323 rtc_irq_data = 0;
324 spin_unlock_irq (&rtc_lock);
326 if (data != 0)
327 break;
329 if (file->f_flags & O_NONBLOCK) {
330 retval = -EAGAIN;
331 goto out;
333 if (signal_pending(current)) {
334 retval = -ERESTARTSYS;
335 goto out;
337 schedule();
338 } while (1);
340 if (count < sizeof(unsigned long))
341 retval = put_user(data, (unsigned int *)buf) ?: sizeof(int);
342 else
343 retval = put_user(data, (unsigned long *)buf) ?: sizeof(long);
344 out:
345 current->state = TASK_RUNNING;
346 remove_wait_queue(&rtc_wait, &wait);
348 return retval;
349 #endif
352 static int rtc_do_ioctl(unsigned int cmd, unsigned long arg, int kernel)
354 struct rtc_time wtime;
356 #if RTC_IRQ
357 if (rtc_has_irq == 0) {
358 switch (cmd) {
359 case RTC_AIE_OFF:
360 case RTC_AIE_ON:
361 case RTC_PIE_OFF:
362 case RTC_PIE_ON:
363 case RTC_UIE_OFF:
364 case RTC_UIE_ON:
365 case RTC_IRQP_READ:
366 case RTC_IRQP_SET:
367 return -EINVAL;
370 #endif
372 switch (cmd) {
373 #if RTC_IRQ
374 case RTC_AIE_OFF: /* Mask alarm int. enab. bit */
376 mask_rtc_irq_bit(RTC_AIE);
377 return 0;
379 case RTC_AIE_ON: /* Allow alarm interrupts. */
381 set_rtc_irq_bit(RTC_AIE);
382 return 0;
384 case RTC_PIE_OFF: /* Mask periodic int. enab. bit */
386 mask_rtc_irq_bit(RTC_PIE);
387 if (rtc_status & RTC_TIMER_ON) {
388 spin_lock_irq (&rtc_lock);
389 rtc_status &= ~RTC_TIMER_ON;
390 del_timer(&rtc_irq_timer);
391 spin_unlock_irq (&rtc_lock);
393 return 0;
395 case RTC_PIE_ON: /* Allow periodic ints */
399 * We don't really want Joe User enabling more
400 * than 64Hz of interrupts on a multi-user machine.
402 if (!kernel && (rtc_freq > rtc_max_user_freq) &&
403 (!capable(CAP_SYS_RESOURCE)))
404 return -EACCES;
406 if (!(rtc_status & RTC_TIMER_ON)) {
407 spin_lock_irq (&rtc_lock);
408 rtc_irq_timer.expires = jiffies + HZ/rtc_freq + 2*HZ/100;
409 add_timer(&rtc_irq_timer);
410 rtc_status |= RTC_TIMER_ON;
411 spin_unlock_irq (&rtc_lock);
413 set_rtc_irq_bit(RTC_PIE);
414 return 0;
416 case RTC_UIE_OFF: /* Mask ints from RTC updates. */
418 mask_rtc_irq_bit(RTC_UIE);
419 return 0;
421 case RTC_UIE_ON: /* Allow ints for RTC updates. */
423 set_rtc_irq_bit(RTC_UIE);
424 return 0;
426 #endif
427 case RTC_ALM_READ: /* Read the present alarm time */
430 * This returns a struct rtc_time. Reading >= 0xc0
431 * means "don't care" or "match all". Only the tm_hour,
432 * tm_min, and tm_sec values are filled in.
434 memset(&wtime, 0, sizeof(struct rtc_time));
435 get_rtc_alm_time(&wtime);
436 break;
438 case RTC_ALM_SET: /* Store a time into the alarm */
441 * This expects a struct rtc_time. Writing 0xff means
442 * "don't care" or "match all". Only the tm_hour,
443 * tm_min and tm_sec are used.
445 unsigned char hrs, min, sec;
446 struct rtc_time alm_tm;
448 if (copy_from_user(&alm_tm, (struct rtc_time*)arg,
449 sizeof(struct rtc_time)))
450 return -EFAULT;
452 hrs = alm_tm.tm_hour;
453 min = alm_tm.tm_min;
454 sec = alm_tm.tm_sec;
456 spin_lock_irq(&rtc_lock);
457 if (hpet_set_alarm_time(hrs, min, sec)) {
459 * Fallthru and set alarm time in CMOS too,
460 * so that we will get proper value in RTC_ALM_READ
463 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) ||
464 RTC_ALWAYS_BCD)
466 if (sec < 60) BIN_TO_BCD(sec);
467 else sec = 0xff;
469 if (min < 60) BIN_TO_BCD(min);
470 else min = 0xff;
472 if (hrs < 24) BIN_TO_BCD(hrs);
473 else hrs = 0xff;
475 CMOS_WRITE(hrs, RTC_HOURS_ALARM);
476 CMOS_WRITE(min, RTC_MINUTES_ALARM);
477 CMOS_WRITE(sec, RTC_SECONDS_ALARM);
478 spin_unlock_irq(&rtc_lock);
480 return 0;
482 case RTC_RD_TIME: /* Read the time/date from RTC */
484 memset(&wtime, 0, sizeof(struct rtc_time));
485 rtc_get_rtc_time(&wtime);
486 break;
488 case RTC_SET_TIME: /* Set the RTC */
490 struct rtc_time rtc_tm;
491 unsigned char mon, day, hrs, min, sec, leap_yr;
492 unsigned char save_control, save_freq_select;
493 unsigned int yrs;
494 #ifdef CONFIG_DECSTATION
495 unsigned int real_yrs;
496 #endif
498 if (!capable(CAP_SYS_TIME))
499 return -EACCES;
501 if (copy_from_user(&rtc_tm, (struct rtc_time*)arg,
502 sizeof(struct rtc_time)))
503 return -EFAULT;
505 yrs = rtc_tm.tm_year + 1900;
506 mon = rtc_tm.tm_mon + 1; /* tm_mon starts at zero */
507 day = rtc_tm.tm_mday;
508 hrs = rtc_tm.tm_hour;
509 min = rtc_tm.tm_min;
510 sec = rtc_tm.tm_sec;
512 if (yrs < 1970)
513 return -EINVAL;
515 leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
517 if ((mon > 12) || (day == 0))
518 return -EINVAL;
520 if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
521 return -EINVAL;
523 if ((hrs >= 24) || (min >= 60) || (sec >= 60))
524 return -EINVAL;
526 if ((yrs -= epoch) > 255) /* They are unsigned */
527 return -EINVAL;
529 spin_lock_irq(&rtc_lock);
530 #ifdef CONFIG_DECSTATION
531 real_yrs = yrs;
532 yrs = 72;
535 * We want to keep the year set to 73 until March
536 * for non-leap years, so that Feb, 29th is handled
537 * correctly.
539 if (!leap_yr && mon < 3) {
540 real_yrs--;
541 yrs = 73;
543 #endif
544 /* These limits and adjustments are independent of
545 * whether the chip is in binary mode or not.
547 if (yrs > 169) {
548 spin_unlock_irq(&rtc_lock);
549 return -EINVAL;
551 if (yrs >= 100)
552 yrs -= 100;
554 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY)
555 || RTC_ALWAYS_BCD) {
556 BIN_TO_BCD(sec);
557 BIN_TO_BCD(min);
558 BIN_TO_BCD(hrs);
559 BIN_TO_BCD(day);
560 BIN_TO_BCD(mon);
561 BIN_TO_BCD(yrs);
564 save_control = CMOS_READ(RTC_CONTROL);
565 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
566 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
567 CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
569 #ifdef CONFIG_DECSTATION
570 CMOS_WRITE(real_yrs, RTC_DEC_YEAR);
571 #endif
572 CMOS_WRITE(yrs, RTC_YEAR);
573 CMOS_WRITE(mon, RTC_MONTH);
574 CMOS_WRITE(day, RTC_DAY_OF_MONTH);
575 CMOS_WRITE(hrs, RTC_HOURS);
576 CMOS_WRITE(min, RTC_MINUTES);
577 CMOS_WRITE(sec, RTC_SECONDS);
579 CMOS_WRITE(save_control, RTC_CONTROL);
580 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
582 spin_unlock_irq(&rtc_lock);
583 return 0;
585 #if RTC_IRQ
586 case RTC_IRQP_READ: /* Read the periodic IRQ rate. */
588 return put_user(rtc_freq, (unsigned long *)arg);
590 case RTC_IRQP_SET: /* Set periodic IRQ rate. */
592 int tmp = 0;
593 unsigned char val;
596 * The max we can do is 8192Hz.
598 if ((arg < 2) || (arg > 8192))
599 return -EINVAL;
601 * We don't really want Joe User generating more
602 * than 64Hz of interrupts on a multi-user machine.
604 if (!kernel && (arg > rtc_max_user_freq) && (!capable(CAP_SYS_RESOURCE)))
605 return -EACCES;
607 while (arg > (1<<tmp))
608 tmp++;
611 * Check that the input was really a power of 2.
613 if (arg != (1<<tmp))
614 return -EINVAL;
616 spin_lock_irq(&rtc_lock);
617 if (hpet_set_periodic_freq(arg)) {
618 spin_unlock_irq(&rtc_lock);
619 return 0;
621 rtc_freq = arg;
623 val = CMOS_READ(RTC_FREQ_SELECT) & 0xf0;
624 val |= (16 - tmp);
625 CMOS_WRITE(val, RTC_FREQ_SELECT);
626 spin_unlock_irq(&rtc_lock);
627 return 0;
629 #endif
630 case RTC_EPOCH_READ: /* Read the epoch. */
632 return put_user (epoch, (unsigned long *)arg);
634 case RTC_EPOCH_SET: /* Set the epoch. */
637 * There were no RTC clocks before 1900.
639 if (arg < 1900)
640 return -EINVAL;
642 if (!capable(CAP_SYS_TIME))
643 return -EACCES;
645 epoch = arg;
646 return 0;
648 default:
649 return -ENOTTY;
651 return copy_to_user((void *)arg, &wtime, sizeof wtime) ? -EFAULT : 0;
654 static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
655 unsigned long arg)
657 return rtc_do_ioctl(cmd, arg, 0);
661 * We enforce only one user at a time here with the open/close.
662 * Also clear the previous interrupt data on an open, and clean
663 * up things on a close.
666 /* We use rtc_lock to protect against concurrent opens. So the BKL is not
667 * needed here. Or anywhere else in this driver. */
668 static int rtc_open(struct inode *inode, struct file *file)
670 spin_lock_irq (&rtc_lock);
672 if(rtc_status & RTC_IS_OPEN)
673 goto out_busy;
675 rtc_status |= RTC_IS_OPEN;
677 rtc_irq_data = 0;
678 spin_unlock_irq (&rtc_lock);
679 return 0;
681 out_busy:
682 spin_unlock_irq (&rtc_lock);
683 return -EBUSY;
686 static int rtc_fasync (int fd, struct file *filp, int on)
689 return fasync_helper (fd, filp, on, &rtc_async_queue);
692 static int rtc_release(struct inode *inode, struct file *file)
694 #if RTC_IRQ
695 unsigned char tmp;
697 if (rtc_has_irq == 0)
698 goto no_irq;
701 * Turn off all interrupts once the device is no longer
702 * in use, and clear the data.
705 spin_lock_irq(&rtc_lock);
706 if (!hpet_mask_rtc_irq_bit(RTC_PIE | RTC_AIE | RTC_UIE)) {
707 tmp = CMOS_READ(RTC_CONTROL);
708 tmp &= ~RTC_PIE;
709 tmp &= ~RTC_AIE;
710 tmp &= ~RTC_UIE;
711 CMOS_WRITE(tmp, RTC_CONTROL);
712 CMOS_READ(RTC_INTR_FLAGS);
714 if (rtc_status & RTC_TIMER_ON) {
715 rtc_status &= ~RTC_TIMER_ON;
716 del_timer(&rtc_irq_timer);
718 spin_unlock_irq(&rtc_lock);
720 if (file->f_flags & FASYNC) {
721 rtc_fasync (-1, file, 0);
723 no_irq:
724 #endif
726 spin_lock_irq (&rtc_lock);
727 rtc_irq_data = 0;
728 rtc_status &= ~RTC_IS_OPEN;
729 spin_unlock_irq (&rtc_lock);
730 return 0;
733 #if RTC_IRQ
734 /* Called without the kernel lock - fine */
735 static unsigned int rtc_poll(struct file *file, poll_table *wait)
737 unsigned long l;
739 if (rtc_has_irq == 0)
740 return 0;
742 poll_wait(file, &rtc_wait, wait);
744 spin_lock_irq (&rtc_lock);
745 l = rtc_irq_data;
746 spin_unlock_irq (&rtc_lock);
748 if (l != 0)
749 return POLLIN | POLLRDNORM;
750 return 0;
752 #endif
755 * exported stuffs
758 EXPORT_SYMBOL(rtc_register);
759 EXPORT_SYMBOL(rtc_unregister);
760 EXPORT_SYMBOL(rtc_control);
762 int rtc_register(rtc_task_t *task)
764 #if !RTC_IRQ
765 return -EIO;
766 #else
767 if (task == NULL || task->func == NULL)
768 return -EINVAL;
769 spin_lock_irq(&rtc_lock);
770 if (rtc_status & RTC_IS_OPEN) {
771 spin_unlock_irq(&rtc_lock);
772 return -EBUSY;
774 spin_lock(&rtc_task_lock);
775 if (rtc_callback) {
776 spin_unlock(&rtc_task_lock);
777 spin_unlock_irq(&rtc_lock);
778 return -EBUSY;
780 rtc_status |= RTC_IS_OPEN;
781 rtc_callback = task;
782 spin_unlock(&rtc_task_lock);
783 spin_unlock_irq(&rtc_lock);
784 return 0;
785 #endif
788 int rtc_unregister(rtc_task_t *task)
790 #if !RTC_IRQ
791 return -EIO;
792 #else
793 unsigned char tmp;
795 spin_lock_irq(&rtc_lock);
796 spin_lock(&rtc_task_lock);
797 if (rtc_callback != task) {
798 spin_unlock(&rtc_task_lock);
799 spin_unlock_irq(&rtc_lock);
800 return -ENXIO;
802 rtc_callback = NULL;
804 /* disable controls */
805 if (!hpet_mask_rtc_irq_bit(RTC_PIE | RTC_AIE | RTC_UIE)) {
806 tmp = CMOS_READ(RTC_CONTROL);
807 tmp &= ~RTC_PIE;
808 tmp &= ~RTC_AIE;
809 tmp &= ~RTC_UIE;
810 CMOS_WRITE(tmp, RTC_CONTROL);
811 CMOS_READ(RTC_INTR_FLAGS);
813 if (rtc_status & RTC_TIMER_ON) {
814 rtc_status &= ~RTC_TIMER_ON;
815 del_timer(&rtc_irq_timer);
817 rtc_status &= ~RTC_IS_OPEN;
818 spin_unlock(&rtc_task_lock);
819 spin_unlock_irq(&rtc_lock);
820 return 0;
821 #endif
824 int rtc_control(rtc_task_t *task, unsigned int cmd, unsigned long arg)
826 #if !RTC_IRQ
827 return -EIO;
828 #else
829 spin_lock_irq(&rtc_task_lock);
830 if (rtc_callback != task) {
831 spin_unlock_irq(&rtc_task_lock);
832 return -ENXIO;
834 spin_unlock_irq(&rtc_task_lock);
835 return rtc_do_ioctl(cmd, arg, 1);
836 #endif
841 * The various file operations we support.
844 static struct file_operations rtc_fops = {
845 .owner = THIS_MODULE,
846 .llseek = no_llseek,
847 .read = rtc_read,
848 #if RTC_IRQ
849 .poll = rtc_poll,
850 #endif
851 .ioctl = rtc_ioctl,
852 .open = rtc_open,
853 .release = rtc_release,
854 .fasync = rtc_fasync,
857 static struct miscdevice rtc_dev=
859 RTC_MINOR,
860 "rtc",
861 &rtc_fops
864 #if RTC_IRQ
865 static irqreturn_t (*rtc_int_handler_ptr)(int irq, void *dev_id, struct pt_regs *regs);
866 #endif
868 static int __init rtc_init(void)
870 #if defined(__alpha__) || defined(__mips__)
871 unsigned int year, ctrl;
872 unsigned long uip_watchdog;
873 char *guess = NULL;
874 #endif
875 #ifdef __sparc__
876 struct linux_ebus *ebus;
877 struct linux_ebus_device *edev;
878 #ifdef __sparc_v9__
879 struct sparc_isa_bridge *isa_br;
880 struct sparc_isa_device *isa_dev;
881 #endif
882 #endif
884 #ifdef __sparc__
885 for_each_ebus(ebus) {
886 for_each_ebusdev(edev, ebus) {
887 if(strcmp(edev->prom_name, "rtc") == 0) {
888 rtc_port = edev->resource[0].start;
889 rtc_irq = edev->irqs[0];
890 goto found;
894 #ifdef __sparc_v9__
895 for_each_isa(isa_br) {
896 for_each_isadev(isa_dev, isa_br) {
897 if (strcmp(isa_dev->prom_name, "rtc") == 0) {
898 rtc_port = isa_dev->resource.start;
899 rtc_irq = isa_dev->irq;
900 goto found;
904 #endif
905 printk(KERN_ERR "rtc_init: no PC rtc found\n");
906 return -EIO;
908 found:
909 if (rtc_irq == PCI_IRQ_NONE) {
910 rtc_has_irq = 0;
911 goto no_irq;
915 * XXX Interrupt pin #7 in Espresso is shared between RTC and
916 * PCI Slot 2 INTA# (and some INTx# in Slot 1). SA_INTERRUPT here
917 * is asking for trouble with add-on boards. Change to SA_SHIRQ.
919 if (request_irq(rtc_irq, rtc_interrupt, SA_INTERRUPT, "rtc", (void *)&rtc_port)) {
921 * Standard way for sparc to print irq's is to use
922 * __irq_itoa(). I think for EBus it's ok to use %d.
924 printk(KERN_ERR "rtc: cannot register IRQ %d\n", rtc_irq);
925 return -EIO;
927 no_irq:
928 #else
929 if (!request_region(RTC_PORT(0), RTC_IO_EXTENT, "rtc")) {
930 printk(KERN_ERR "rtc: I/O port %d is not free.\n", RTC_PORT (0));
931 return -EIO;
934 #if RTC_IRQ
935 if (is_hpet_enabled()) {
936 rtc_int_handler_ptr = hpet_rtc_interrupt;
937 } else {
938 rtc_int_handler_ptr = rtc_interrupt;
941 if(request_irq(RTC_IRQ, rtc_int_handler_ptr, SA_INTERRUPT, "rtc", NULL)) {
942 /* Yeah right, seeing as irq 8 doesn't even hit the bus. */
943 printk(KERN_ERR "rtc: IRQ %d is not free.\n", RTC_IRQ);
944 release_region(RTC_PORT(0), RTC_IO_EXTENT);
945 return -EIO;
947 hpet_rtc_timer_init();
949 #endif
951 #endif /* __sparc__ vs. others */
953 if (misc_register(&rtc_dev)) {
954 #if RTC_IRQ
955 free_irq(RTC_IRQ, NULL);
956 #endif
957 release_region(RTC_PORT(0), RTC_IO_EXTENT);
958 return -ENODEV;
960 if (create_proc_read_entry ("driver/rtc", 0, 0, rtc_read_proc, NULL) == NULL) {
961 #if RTC_IRQ
962 free_irq(RTC_IRQ, NULL);
963 #endif
964 release_region(RTC_PORT(0), RTC_IO_EXTENT);
965 misc_deregister(&rtc_dev);
966 return -ENOMEM;
969 #if defined(__alpha__) || defined(__mips__)
970 rtc_freq = HZ;
972 /* Each operating system on an Alpha uses its own epoch.
973 Let's try to guess which one we are using now. */
975 uip_watchdog = jiffies;
976 if (rtc_is_updating() != 0)
977 while (jiffies - uip_watchdog < 2*HZ/100) {
978 barrier();
979 cpu_relax();
982 spin_lock_irq(&rtc_lock);
983 year = CMOS_READ(RTC_YEAR);
984 ctrl = CMOS_READ(RTC_CONTROL);
985 spin_unlock_irq(&rtc_lock);
987 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
988 BCD_TO_BIN(year); /* This should never happen... */
990 if (year < 20) {
991 epoch = 2000;
992 guess = "SRM (post-2000)";
993 } else if (year >= 20 && year < 48) {
994 epoch = 1980;
995 guess = "ARC console";
996 } else if (year >= 48 && year < 72) {
997 epoch = 1952;
998 guess = "Digital UNIX";
999 #if defined(__mips__)
1000 } else if (year >= 72 && year < 74) {
1001 epoch = 2000;
1002 guess = "Digital DECstation";
1003 #else
1004 } else if (year >= 70) {
1005 epoch = 1900;
1006 guess = "Standard PC (1900)";
1007 #endif
1009 if (guess)
1010 printk(KERN_INFO "rtc: %s epoch (%lu) detected\n", guess, epoch);
1011 #endif
1012 #if RTC_IRQ
1013 if (rtc_has_irq == 0)
1014 goto no_irq2;
1016 init_timer(&rtc_irq_timer);
1017 rtc_irq_timer.function = rtc_dropped_irq;
1018 spin_lock_irq(&rtc_lock);
1019 rtc_freq = 1024;
1020 if (!hpet_set_periodic_freq(rtc_freq)) {
1021 /* Initialize periodic freq. to CMOS reset default, which is 1024Hz */
1022 CMOS_WRITE(((CMOS_READ(RTC_FREQ_SELECT) & 0xF0) | 0x06), RTC_FREQ_SELECT);
1024 spin_unlock_irq(&rtc_lock);
1025 no_irq2:
1026 #endif
1028 (void) init_sysctl();
1030 printk(KERN_INFO "Real Time Clock Driver v" RTC_VERSION "\n");
1032 return 0;
1035 static void __exit rtc_exit (void)
1037 cleanup_sysctl();
1038 remove_proc_entry ("driver/rtc", NULL);
1039 misc_deregister(&rtc_dev);
1041 #ifdef __sparc__
1042 if (rtc_has_irq)
1043 free_irq (rtc_irq, &rtc_port);
1044 #else
1045 release_region (RTC_PORT (0), RTC_IO_EXTENT);
1046 #if RTC_IRQ
1047 if (rtc_has_irq)
1048 free_irq (RTC_IRQ, NULL);
1049 #endif
1050 #endif /* __sparc__ */
1053 module_init(rtc_init);
1054 module_exit(rtc_exit);
1056 #if RTC_IRQ
1058 * At IRQ rates >= 4096Hz, an interrupt may get lost altogether.
1059 * (usually during an IDE disk interrupt, with IRQ unmasking off)
1060 * Since the interrupt handler doesn't get called, the IRQ status
1061 * byte doesn't get read, and the RTC stops generating interrupts.
1062 * A timer is set, and will call this function if/when that happens.
1063 * To get it out of this stalled state, we just read the status.
1064 * At least a jiffy of interrupts (rtc_freq/HZ) will have been lost.
1065 * (You *really* shouldn't be trying to use a non-realtime system
1066 * for something that requires a steady > 1KHz signal anyways.)
1069 static void rtc_dropped_irq(unsigned long data)
1071 unsigned long freq;
1073 spin_lock_irq (&rtc_lock);
1075 if (hpet_rtc_dropped_irq()) {
1076 spin_unlock_irq(&rtc_lock);
1077 return;
1080 /* Just in case someone disabled the timer from behind our back... */
1081 if (rtc_status & RTC_TIMER_ON)
1082 mod_timer(&rtc_irq_timer, jiffies + HZ/rtc_freq + 2*HZ/100);
1084 rtc_irq_data += ((rtc_freq/HZ)<<8);
1085 rtc_irq_data &= ~0xff;
1086 rtc_irq_data |= (CMOS_READ(RTC_INTR_FLAGS) & 0xF0); /* restart */
1088 freq = rtc_freq;
1090 spin_unlock_irq(&rtc_lock);
1092 printk(KERN_WARNING "rtc: lost some interrupts at %ldHz.\n", freq);
1094 /* Now we have new data */
1095 wake_up_interruptible(&rtc_wait);
1097 kill_fasync (&rtc_async_queue, SIGIO, POLL_IN);
1099 #endif
1102 * Info exported via "/proc/driver/rtc".
1105 static int rtc_proc_output (char *buf)
1107 #define YN(bit) ((ctrl & bit) ? "yes" : "no")
1108 #define NY(bit) ((ctrl & bit) ? "no" : "yes")
1109 char *p;
1110 struct rtc_time tm;
1111 unsigned char batt, ctrl;
1112 unsigned long freq;
1114 spin_lock_irq(&rtc_lock);
1115 batt = CMOS_READ(RTC_VALID) & RTC_VRT;
1116 ctrl = CMOS_READ(RTC_CONTROL);
1117 freq = rtc_freq;
1118 spin_unlock_irq(&rtc_lock);
1120 p = buf;
1122 rtc_get_rtc_time(&tm);
1125 * There is no way to tell if the luser has the RTC set for local
1126 * time or for Universal Standard Time (GMT). Probably local though.
1128 p += sprintf(p,
1129 "rtc_time\t: %02d:%02d:%02d\n"
1130 "rtc_date\t: %04d-%02d-%02d\n"
1131 "rtc_epoch\t: %04lu\n",
1132 tm.tm_hour, tm.tm_min, tm.tm_sec,
1133 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, epoch);
1135 get_rtc_alm_time(&tm);
1138 * We implicitly assume 24hr mode here. Alarm values >= 0xc0 will
1139 * match any value for that particular field. Values that are
1140 * greater than a valid time, but less than 0xc0 shouldn't appear.
1142 p += sprintf(p, "alarm\t\t: ");
1143 if (tm.tm_hour <= 24)
1144 p += sprintf(p, "%02d:", tm.tm_hour);
1145 else
1146 p += sprintf(p, "**:");
1148 if (tm.tm_min <= 59)
1149 p += sprintf(p, "%02d:", tm.tm_min);
1150 else
1151 p += sprintf(p, "**:");
1153 if (tm.tm_sec <= 59)
1154 p += sprintf(p, "%02d\n", tm.tm_sec);
1155 else
1156 p += sprintf(p, "**\n");
1158 p += sprintf(p,
1159 "DST_enable\t: %s\n"
1160 "BCD\t\t: %s\n"
1161 "24hr\t\t: %s\n"
1162 "square_wave\t: %s\n"
1163 "alarm_IRQ\t: %s\n"
1164 "update_IRQ\t: %s\n"
1165 "periodic_IRQ\t: %s\n"
1166 "periodic_freq\t: %ld\n"
1167 "batt_status\t: %s\n",
1168 YN(RTC_DST_EN),
1169 NY(RTC_DM_BINARY),
1170 YN(RTC_24H),
1171 YN(RTC_SQWE),
1172 YN(RTC_AIE),
1173 YN(RTC_UIE),
1174 YN(RTC_PIE),
1175 freq,
1176 batt ? "okay" : "dead");
1178 return p - buf;
1179 #undef YN
1180 #undef NY
1183 static int rtc_read_proc(char *page, char **start, off_t off,
1184 int count, int *eof, void *data)
1186 int len = rtc_proc_output (page);
1187 if (len <= off+count) *eof = 1;
1188 *start = page + off;
1189 len -= off;
1190 if (len>count) len = count;
1191 if (len<0) len = 0;
1192 return len;
1196 * Returns true if a clock update is in progress
1198 /* FIXME shouldn't this be above rtc_init to make it fully inlined? */
1199 static inline unsigned char rtc_is_updating(void)
1201 unsigned char uip;
1203 spin_lock_irq(&rtc_lock);
1204 uip = (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP);
1205 spin_unlock_irq(&rtc_lock);
1206 return uip;
1209 void rtc_get_rtc_time(struct rtc_time *rtc_tm)
1211 unsigned long uip_watchdog = jiffies;
1212 unsigned char ctrl;
1213 #ifdef CONFIG_DECSTATION
1214 unsigned int real_year;
1215 #endif
1218 * read RTC once any update in progress is done. The update
1219 * can take just over 2ms. We wait 10 to 20ms. There is no need to
1220 * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
1221 * If you need to know *exactly* when a second has started, enable
1222 * periodic update complete interrupts, (via ioctl) and then
1223 * immediately read /dev/rtc which will block until you get the IRQ.
1224 * Once the read clears, read the RTC time (again via ioctl). Easy.
1227 if (rtc_is_updating() != 0)
1228 while (jiffies - uip_watchdog < 2*HZ/100) {
1229 barrier();
1230 cpu_relax();
1234 * Only the values that we read from the RTC are set. We leave
1235 * tm_wday, tm_yday and tm_isdst untouched. Even though the
1236 * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
1237 * by the RTC when initially set to a non-zero value.
1239 spin_lock_irq(&rtc_lock);
1240 rtc_tm->tm_sec = CMOS_READ(RTC_SECONDS);
1241 rtc_tm->tm_min = CMOS_READ(RTC_MINUTES);
1242 rtc_tm->tm_hour = CMOS_READ(RTC_HOURS);
1243 rtc_tm->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH);
1244 rtc_tm->tm_mon = CMOS_READ(RTC_MONTH);
1245 rtc_tm->tm_year = CMOS_READ(RTC_YEAR);
1246 #ifdef CONFIG_DECSTATION
1247 real_year = CMOS_READ(RTC_DEC_YEAR);
1248 #endif
1249 ctrl = CMOS_READ(RTC_CONTROL);
1250 spin_unlock_irq(&rtc_lock);
1252 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
1254 BCD_TO_BIN(rtc_tm->tm_sec);
1255 BCD_TO_BIN(rtc_tm->tm_min);
1256 BCD_TO_BIN(rtc_tm->tm_hour);
1257 BCD_TO_BIN(rtc_tm->tm_mday);
1258 BCD_TO_BIN(rtc_tm->tm_mon);
1259 BCD_TO_BIN(rtc_tm->tm_year);
1262 #ifdef CONFIG_DECSTATION
1263 rtc_tm->tm_year += real_year - 72;
1264 #endif
1267 * Account for differences between how the RTC uses the values
1268 * and how they are defined in a struct rtc_time;
1270 if ((rtc_tm->tm_year += (epoch - 1900)) <= 69)
1271 rtc_tm->tm_year += 100;
1273 rtc_tm->tm_mon--;
1276 static void get_rtc_alm_time(struct rtc_time *alm_tm)
1278 unsigned char ctrl;
1281 * Only the values that we read from the RTC are set. That
1282 * means only tm_hour, tm_min, and tm_sec.
1284 spin_lock_irq(&rtc_lock);
1285 alm_tm->tm_sec = CMOS_READ(RTC_SECONDS_ALARM);
1286 alm_tm->tm_min = CMOS_READ(RTC_MINUTES_ALARM);
1287 alm_tm->tm_hour = CMOS_READ(RTC_HOURS_ALARM);
1288 ctrl = CMOS_READ(RTC_CONTROL);
1289 spin_unlock_irq(&rtc_lock);
1291 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
1293 BCD_TO_BIN(alm_tm->tm_sec);
1294 BCD_TO_BIN(alm_tm->tm_min);
1295 BCD_TO_BIN(alm_tm->tm_hour);
1299 #if RTC_IRQ
1301 * Used to disable/enable interrupts for any one of UIE, AIE, PIE.
1302 * Rumour has it that if you frob the interrupt enable/disable
1303 * bits in RTC_CONTROL, you should read RTC_INTR_FLAGS, to
1304 * ensure you actually start getting interrupts. Probably for
1305 * compatibility with older/broken chipset RTC implementations.
1306 * We also clear out any old irq data after an ioctl() that
1307 * meddles with the interrupt enable/disable bits.
1310 static void mask_rtc_irq_bit(unsigned char bit)
1312 unsigned char val;
1314 spin_lock_irq(&rtc_lock);
1315 if (hpet_mask_rtc_irq_bit(bit)) {
1316 spin_unlock_irq(&rtc_lock);
1317 return;
1319 val = CMOS_READ(RTC_CONTROL);
1320 val &= ~bit;
1321 CMOS_WRITE(val, RTC_CONTROL);
1322 CMOS_READ(RTC_INTR_FLAGS);
1324 rtc_irq_data = 0;
1325 spin_unlock_irq(&rtc_lock);
1328 static void set_rtc_irq_bit(unsigned char bit)
1330 unsigned char val;
1332 spin_lock_irq(&rtc_lock);
1333 if (hpet_set_rtc_irq_bit(bit)) {
1334 spin_unlock_irq(&rtc_lock);
1335 return;
1337 val = CMOS_READ(RTC_CONTROL);
1338 val |= bit;
1339 CMOS_WRITE(val, RTC_CONTROL);
1340 CMOS_READ(RTC_INTR_FLAGS);
1342 rtc_irq_data = 0;
1343 spin_unlock_irq(&rtc_lock);
1345 #endif
1347 MODULE_AUTHOR("Paul Gortmaker");
1348 MODULE_LICENSE("GPL");
1349 MODULE_ALIAS_MISCDEV(RTC_MINOR);