1 /*
2 * Real Time Clock interface for Linux
3 *
4 * Copyright (C) 1996 Paul Gortmaker
5 *
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.
10 *
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.
15 *
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.
22 *
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.
27 *
28 * Based on other minimal char device drivers, like Alan's
29 * watchdog, Ted's random, etc. etc.
30 *
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
49 * 1.12a Maciej W. Rozycki: Handle memory-mapped chips properly.
50 * 1.12ac Alan Cox: Allow read access to the day of week register
51 */
52
53 #define RTC_VERSION "1.12ac"
54
55 /*
56 * Note that *all* calls to CMOS_READ and CMOS_WRITE are done with
57 * interrupts disabled. Due to the index-port/data-port (0x70/0x71)
58 * design of the RTC, we don't want two different things trying to
59 * get to it at once. (e.g. the periodic 11 min sync from time.c vs.
60 * this driver.)
61 */
62
63 #include <linux/interrupt.h>
64 #include <linux/module.h>
65 #include <linux/kernel.h>
66 #include <linux/types.h>
67 #include <linux/miscdevice.h>
68 #include <linux/ioport.h>
69 #include <linux/fcntl.h>
70 #include <linux/mc146818rtc.h>
71 #include <linux/init.h>
72 #include <linux/poll.h>
73 #include <linux/proc_fs.h>
74 #include <linux/seq_file.h>
75 #include <linux/spinlock.h>
76 #include <linux/sysctl.h>
77 #include <linux/wait.h>
78 #include <linux/bcd.h>
79 #include <linux/delay.h>
80
81 #include <asm/current.h>
82 #include <asm/uaccess.h>
83 #include <asm/system.h>
84
85 #ifdef CONFIG_X86
86 #include <asm/hpet.h>
87 #endif
88
89 #ifdef CONFIG_SPARC32
90 #include <linux/pci.h>
91 #include <asm/ebus.h>
92
93 static unsigned long rtc_port;
94 static int rtc_irq = PCI_IRQ_NONE;
95 #endif
96
97 #ifdef CONFIG_MIPS
98 # include <asm/time.h>
99 #endif
100
101 #ifdef CONFIG_RTC_HISTOGRAM
102
103 static cycles_t last_interrupt_time;
104
105 #include <asm/timex.h>
106
107 #define CPU_MHZ (cpu_khz / 1000)
108
109 #define HISTSIZE 10000
110 static int histogram[HISTSIZE];
111
112 static int rtc_state;
113
114 enum rtc_states {
115 S_STARTUP, /* First round - let the application start */
116 S_IDLE, /* Waiting for an interrupt */
117 S_WAITING_FOR_READ, /* Signal delivered. waiting for rtc_read() */
118 S_READ_MISSED, /* Signal delivered, read() deadline missed */
119 };
120
121 #endif
122
123 #ifdef CONFIG_HPET_RTC_IRQ
124 #undef RTC_IRQ
125 #endif
126
127 #ifdef RTC_IRQ
128 static int rtc_has_irq = 1;
129 #endif
130
131 #ifndef CONFIG_HPET_EMULATE_RTC
132 #define is_hpet_enabled() 0
133 #define hpet_set_alarm_time(hrs, min, sec) 0
134 #define hpet_set_periodic_freq(arg) 0
135 #define hpet_mask_rtc_irq_bit(arg) 0
136 #define hpet_set_rtc_irq_bit(arg) 0
137 #define hpet_rtc_timer_init() do { } while (0)
138 #define hpet_rtc_dropped_irq() 0
139 #define hpet_register_irq_handler(h) ({ 0; })
140 #define hpet_unregister_irq_handler(h) ({ 0; })
141 #ifdef RTC_IRQ
142 static irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id)
143 {
144 return 0;
145 }
146 #endif
147 #else
148 extern irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id);
149 #endif
150
151 /*
152 * We sponge a minor off of the misc major. No need slurping
153 * up another valuable major dev number for this. If you add
154 * an ioctl, make sure you don't conflict with SPARC's RTC
155 * ioctls.
156 */
157
158 static struct fasync_struct *rtc_async_queue;
159
160 static DECLARE_WAIT_QUEUE_HEAD(rtc_wait);
161
162 #ifdef RTC_IRQ
163 static void rtc_dropped_irq(unsigned long data);
164
165 static DEFINE_TIMER(rtc_irq_timer, rtc_dropped_irq, 0, 0);
166 #endif
167
168 static ssize_t rtc_read(struct file *file, char __user *buf,
169 size_t count, loff_t *ppos);
170
171 static int rtc_ioctl(struct inode *inode, struct file *file,
172 unsigned int cmd, unsigned long arg);
173
174 #ifdef RTC_IRQ
175 static unsigned int rtc_poll(struct file *file, poll_table *wait);
176 #endif
177
178 static void get_rtc_alm_time(struct rtc_time *alm_tm);
179 #ifdef RTC_IRQ
180 static void set_rtc_irq_bit_locked(unsigned char bit);
181 static void mask_rtc_irq_bit_locked(unsigned char bit);
182
183 static inline void set_rtc_irq_bit(unsigned char bit)
184 {
185 spin_lock_irq(&rtc_lock);
186 set_rtc_irq_bit_locked(bit);
187 spin_unlock_irq(&rtc_lock);
188 }
189
190 static void mask_rtc_irq_bit(unsigned char bit)
191 {
192 spin_lock_irq(&rtc_lock);
193 mask_rtc_irq_bit_locked(bit);
194 spin_unlock_irq(&rtc_lock);
195 }
196 #endif
197
198 #ifdef CONFIG_PROC_FS
199 static int rtc_proc_open(struct inode *inode, struct file *file);
200 #endif
201
202 /*
203 * Bits in rtc_status. (6 bits of room for future expansion)
204 */
205
206 #define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
207 #define RTC_TIMER_ON 0x02 /* missed irq timer active */
208
209 /*
210 * rtc_status is never changed by rtc_interrupt, and ioctl/open/close is
211 * protected by the big kernel lock. However, ioctl can still disable the timer
212 * in rtc_status and then with del_timer after the interrupt has read
213 * rtc_status but before mod_timer is called, which would then reenable the
214 * timer (but you would need to have an awful timing before you'd trip on it)
215 */
216 static unsigned long rtc_status; /* bitmapped status byte. */
217 static unsigned long rtc_freq; /* Current periodic IRQ rate */
218 static unsigned long rtc_irq_data; /* our output to the world */
219 static unsigned long rtc_max_user_freq = 64; /* > this, need CAP_SYS_RESOURCE */
220
221 #ifdef RTC_IRQ
222 /*
223 * rtc_task_lock nests inside rtc_lock.
224 */
225 static DEFINE_SPINLOCK(rtc_task_lock);
226 static rtc_task_t *rtc_callback;
227 #endif
228
229 /*
230 * If this driver ever becomes modularised, it will be really nice
231 * to make the epoch retain its value across module reload...
232 */
233
234 static unsigned long epoch = 1900; /* year corresponding to 0x00 */
235
236 static const unsigned char days_in_mo[] =
237 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
238
239 /*
240 * Returns true if a clock update is in progress
241 */
242 static inline unsigned char rtc_is_updating(void)
243 {
244 unsigned long flags;
245 unsigned char uip;
246
247 spin_lock_irqsave(&rtc_lock, flags);
248 uip = (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP);
249 spin_unlock_irqrestore(&rtc_lock, flags);
250 return uip;
251 }
252
253 #ifndef RTC_IRQ
254 # undef CONFIG_RTC_HISTOGRAM
255 #endif
256
257 static inline void rtc_open_event(void)
258 {
259 #ifdef CONFIG_RTC_HISTOGRAM
260 int i;
261
262 last_interrupt_time = 0;
263 rtc_state = S_STARTUP;
264 rtc_irq_data = 0;
265
266 for (i = 0; i < HISTSIZE; i++)
267 histogram[i] = 0;
268 #endif
269 }
270
271 static inline void rtc_wake_event(void)
272 {
273 #ifndef CONFIG_RTC_HISTOGRAM
274 kill_fasync (&rtc_async_queue, SIGIO, POLL_IN);
275 #else
276 if (!(rtc_status & RTC_IS_OPEN))
277 return;
278
279 switch (rtc_state) {
280 /* Startup */
281 case S_STARTUP:
282 kill_fasync (&rtc_async_queue, SIGIO, POLL_IN);
283 break;
284 /* Waiting for an interrupt */
285 case S_IDLE:
286 kill_fasync (&rtc_async_queue, SIGIO, POLL_IN);
287 last_interrupt_time = get_cycles();
288 rtc_state = S_WAITING_FOR_READ;
289 break;
290
291 /* Signal has been delivered. waiting for rtc_read() */
292 case S_WAITING_FOR_READ:
293 /*
294 * Well foo. The usermode application didn't
295 * schedule and read in time.
296 */
297 last_interrupt_time = get_cycles();
298 rtc_state = S_READ_MISSED;
299 printk("Read missed before next interrupt\n");
300 break;
301 /* Signal has been delivered, read() deadline was missed */
302 case S_READ_MISSED:
303 /*
304 * Not much we can do here. We're waiting for the usermode
305 * application to read the rtc
306 */
307 last_interrupt_time = get_cycles();
308 break;
309 }
310 #endif
311 }
312
313 static inline void rtc_read_event(void)
314 {
315 #ifdef CONFIG_RTC_HISTOGRAM
316 cycles_t now = get_cycles();
317
318 switch (rtc_state) {
319 /* Startup */
320 case S_STARTUP:
321 rtc_state = S_IDLE;
322 break;
323
324 /* Waiting for an interrupt */
325 case S_IDLE:
326 printk("bug in rtc_read(): called in state S_IDLE!\n");
327 break;
328 case S_WAITING_FOR_READ: /*
329 * Signal has been delivered.
330 * waiting for rtc_read()
331 */
332 /*
333 * Well done
334 */
335 case S_READ_MISSED: /*
336 * Signal has been delivered, read()
337 * deadline was missed
338 */
339 /*
340 * So, you finally got here.
341 */
342 if (!last_interrupt_time)
343 printk("bug in rtc_read(): last_interrupt_time = 0\n");
344 rtc_state = S_IDLE;
345 {
346 cycles_t latency = now - last_interrupt_time;
347 unsigned long delta; /* Microseconds */
348
349 delta = latency;
350 delta /= CPU_MHZ;
351
352 if (delta > 1000 * 1000) {
353 printk("rtc: eek\n");
354 } else {
355 unsigned long slot = delta;
356 if (slot >= HISTSIZE)
357 slot = HISTSIZE - 1;
358 histogram[slot]++;
359 if (delta > 2000)
360 printk("wow! That was a "
361 "%ld millisec bump\n",
362 delta / 1000);
363 }
364 }
365 rtc_state = S_IDLE;
366 break;
367 }
368 #endif
369 }
370
371 static inline void rtc_close_event(void)
372 {
373 #ifdef CONFIG_RTC_HISTOGRAM
374 int i = 0;
375 unsigned long total = 0;
376
377 for (i = 0; i < HISTSIZE; i++)
378 total += histogram[i];
379 if (!total)
380 return;
381
382 printk("\nrtc latency histogram of {%s/%d, %lu samples}:\n",
383 current->comm, current->pid, total);
384 for (i = 0; i < HISTSIZE; i++) {
385 if (histogram[i])
386 printk("%d %d\n", i, histogram[i]);
387 }
388 #endif
389 }
390
391 #ifdef RTC_IRQ
392
393 /*
394 * A very tiny interrupt handler. It runs with IRQF_DISABLED set,
395 * but there is possibility of conflicting with the set_rtc_mmss()
396 * call (the rtc irq and the timer irq can easily run at the same
397 * time in two different CPUs). So we need to serialize
398 * accesses to the chip with the rtc_lock spinlock that each
399 * architecture should implement in the timer code.
400 * (See ./arch/XXXX/kernel/time.c for the set_rtc_mmss() function.)
401 */
402
403 irqreturn_t rtc_interrupt(int irq, void *dev_id)
404 {
405 /*
406 * Can be an alarm interrupt, update complete interrupt,
407 * or a periodic interrupt. We store the status in the
408 * low byte and the number of interrupts received since
409 * the last read in the remainder of rtc_irq_data.
410 */
411
412 spin_lock(&rtc_lock);
413 rtc_irq_data += 0x100;
414 rtc_irq_data &= ~0xff;
415 if (is_hpet_enabled()) {
416 /*
417 * In this case it is HPET RTC interrupt handler
418 * calling us, with the interrupt information
419 * passed as arg1, instead of irq.
420 */
421 rtc_irq_data |= (unsigned long)irq & 0xF0;
422 } else {
423 rtc_irq_data |= (CMOS_READ(RTC_INTR_FLAGS) & 0xF0);
424 }
425
426 if (rtc_status & RTC_TIMER_ON)
427 mod_timer(&rtc_irq_timer, jiffies + HZ/rtc_freq + 2*HZ/100);
428
429 spin_unlock(&rtc_lock);
430
431 /* Now do the rest of the actions */
432 spin_lock(&rtc_task_lock);
433 if (rtc_callback)
434 rtc_callback->func(rtc_callback->private_data);
435 spin_unlock(&rtc_task_lock);
436
437 rtc_wake_event();
438 wake_up_interruptible(&rtc_wait);
439
440 return IRQ_HANDLED;
441 }
442 #endif
443
444 /*
445 * sysctl-tuning infrastructure.
446 */
447 static ctl_table rtc_table[] = {
448 {
449 .ctl_name = CTL_UNNUMBERED,
450 .procname = "max-user-freq",
451 .data = &rtc_max_user_freq,
452 .maxlen = sizeof(int),
453 .mode = 0644,
454 .proc_handler = &proc_dointvec,
455 },
456 { .ctl_name = 0 }
457 };
458
459 static ctl_table rtc_root[] = {
460 {
461 .ctl_name = CTL_UNNUMBERED,
462 .procname = "rtc",
463 .mode = 0555,
464 .child = rtc_table,
465 },
466 { .ctl_name = 0 }
467 };
468
469 static ctl_table dev_root[] = {
470 {
471 .ctl_name = CTL_DEV,
472 .procname = "dev",
473 .mode = 0555,
474 .child = rtc_root,
475 },
476 { .ctl_name = 0 }
477 };
478
479 static struct ctl_table_header *sysctl_header;
480
481 static int __init init_sysctl(void)
482 {
483 sysctl_header = register_sysctl_table(dev_root);
484 return 0;
485 }
486
487 static void __exit cleanup_sysctl(void)
488 {
489 unregister_sysctl_table(sysctl_header);
490 }
491
492 /*
493 * Now all the various file operations that we export.
494 */
495
496 static ssize_t rtc_read(struct file *file, char __user *buf,
497 size_t count, loff_t *ppos)
498 {
499 #ifndef RTC_IRQ
500 return -EIO;
501 #else
502 DECLARE_WAITQUEUE(wait, current);
503 unsigned long data;
504 ssize_t retval;
505
506 if (rtc_has_irq == 0)
507 return -EIO;
508
509 /*
510 * Historically this function used to assume that sizeof(unsigned long)
511 * is the same in userspace and kernelspace. This lead to problems
512 * for configurations with multiple ABIs such a the MIPS o32 and 64
513 * ABIs supported on the same kernel. So now we support read of both
514 * 4 and 8 bytes and assume that's the sizeof(unsigned long) in the
515 * userspace ABI.
516 */
517 if (count != sizeof(unsigned int) && count != sizeof(unsigned long))
518 return -EINVAL;
519
520 add_wait_queue(&rtc_wait, &wait);
521
522 do {
523 /* First make it right. Then make it fast. Putting this whole
524 * block within the parentheses of a while would be too
525 * confusing. And no, xchg() is not the answer. */
526
527 __set_current_state(TASK_INTERRUPTIBLE);
528
529 spin_lock_irq(&rtc_lock);
530 data = rtc_irq_data;
531 rtc_irq_data = 0;
532 spin_unlock_irq(&rtc_lock);
533
534 if (data != 0)
535 break;
536
537 if (file->f_flags & O_NONBLOCK) {
538 retval = -EAGAIN;
539 goto out;
540 }
541 if (signal_pending(current)) {
542 retval = -ERESTARTSYS;
543 goto out;
544 }
545 schedule();
546 } while (1);
547
548 rtc_read_event();
549
550 if (count == sizeof(unsigned int)) {
551 retval = put_user(data,
552 (unsigned int __user *)buf) ?: sizeof(int);
553 } else {
554 retval = put_user(data,
555 (unsigned long __user *)buf) ?: sizeof(long);
556 }
557 if (!retval)
558 retval = count;
559 out:
560 __set_current_state(TASK_RUNNING);
561 remove_wait_queue(&rtc_wait, &wait);
562
563 return retval;
564 #endif
565 }
566
567 static int rtc_do_ioctl(unsigned int cmd, unsigned long arg, int kernel)
568 {
569 struct rtc_time wtime;
570
571 #ifdef RTC_IRQ
572 if (rtc_has_irq == 0) {
573 switch (cmd) {
574 case RTC_AIE_OFF:
575 case RTC_AIE_ON:
576 case RTC_PIE_OFF:
577 case RTC_PIE_ON:
578 case RTC_UIE_OFF:
579 case RTC_UIE_ON:
580 case RTC_IRQP_READ:
581 case RTC_IRQP_SET:
582 return -EINVAL;
583 };
584 }
585 #endif
586
587 switch (cmd) {
588 #ifdef RTC_IRQ
589 case RTC_AIE_OFF: /* Mask alarm int. enab. bit */
590 {
591 mask_rtc_irq_bit(RTC_AIE);
592 return 0;
593 }
594 case RTC_AIE_ON: /* Allow alarm interrupts. */
595 {
596 set_rtc_irq_bit(RTC_AIE);
597 return 0;
598 }
599 case RTC_PIE_OFF: /* Mask periodic int. enab. bit */
600 {
601 /* can be called from isr via rtc_control() */
602 unsigned long flags;
603
604 spin_lock_irqsave(&rtc_lock, flags);
605 mask_rtc_irq_bit_locked(RTC_PIE);
606 if (rtc_status & RTC_TIMER_ON) {
607 rtc_status &= ~RTC_TIMER_ON;
608 del_timer(&rtc_irq_timer);
609 }
610 spin_unlock_irqrestore(&rtc_lock, flags);
611
612 return 0;
613 }
614 case RTC_PIE_ON: /* Allow periodic ints */
615 {
616 /* can be called from isr via rtc_control() */
617 unsigned long flags;
618
619 /*
620 * We don't really want Joe User enabling more
621 * than 64Hz of interrupts on a multi-user machine.
622 */
623 if (!kernel && (rtc_freq > rtc_max_user_freq) &&
624 (!capable(CAP_SYS_RESOURCE)))
625 return -EACCES;
626
627 spin_lock_irqsave(&rtc_lock, flags);
628 if (!(rtc_status & RTC_TIMER_ON)) {
629 mod_timer(&rtc_irq_timer, jiffies + HZ/rtc_freq +
630 2*HZ/100);
631 rtc_status |= RTC_TIMER_ON;
632 }
633 set_rtc_irq_bit_locked(RTC_PIE);
634 spin_unlock_irqrestore(&rtc_lock, flags);
635
636 return 0;
637 }
638 case RTC_UIE_OFF: /* Mask ints from RTC updates. */
639 {
640 mask_rtc_irq_bit(RTC_UIE);
641 return 0;
642 }
643 case RTC_UIE_ON: /* Allow ints for RTC updates. */
644 {
645 set_rtc_irq_bit(RTC_UIE);
646 return 0;
647 }
648 #endif
649 case RTC_ALM_READ: /* Read the present alarm time */
650 {
651 /*
652 * This returns a struct rtc_time. Reading >= 0xc0
653 * means "don't care" or "match all". Only the tm_hour,
654 * tm_min, and tm_sec values are filled in.
655 */
656 memset(&wtime, 0, sizeof(struct rtc_time));
657 get_rtc_alm_time(&wtime);
658 break;
659 }
660 case RTC_ALM_SET: /* Store a time into the alarm */
661 {
662 /*
663 * This expects a struct rtc_time. Writing 0xff means
664 * "don't care" or "match all". Only the tm_hour,
665 * tm_min and tm_sec are used.
666 */
667 unsigned char hrs, min, sec;
668 struct rtc_time alm_tm;
669
670 if (copy_from_user(&alm_tm, (struct rtc_time __user *)arg,
671 sizeof(struct rtc_time)))
672 return -EFAULT;
673
674 hrs = alm_tm.tm_hour;
675 min = alm_tm.tm_min;
676 sec = alm_tm.tm_sec;
677
678 spin_lock_irq(&rtc_lock);
679 if (hpet_set_alarm_time(hrs, min, sec)) {
680 /*
681 * Fallthru and set alarm time in CMOS too,
682 * so that we will get proper value in RTC_ALM_READ
683 */
684 }
685 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) ||
686 RTC_ALWAYS_BCD) {
687 if (sec < 60)
688 BIN_TO_BCD(sec);
689 else
690 sec = 0xff;
691
692 if (min < 60)
693 BIN_TO_BCD(min);
694 else
695 min = 0xff;
696
697 if (hrs < 24)
698 BIN_TO_BCD(hrs);
699 else
700 hrs = 0xff;
701 }
702 CMOS_WRITE(hrs, RTC_HOURS_ALARM);
703 CMOS_WRITE(min, RTC_MINUTES_ALARM);
704 CMOS_WRITE(sec, RTC_SECONDS_ALARM);
705 spin_unlock_irq(&rtc_lock);
706
707 return 0;
708 }
709 case RTC_RD_TIME: /* Read the time/date from RTC */
710 {
711 memset(&wtime, 0, sizeof(struct rtc_time));
712 rtc_get_rtc_time(&wtime);
713 break;
714 }
715 case RTC_SET_TIME: /* Set the RTC */
716 {
717 struct rtc_time rtc_tm;
718 unsigned char mon, day, hrs, min, sec, leap_yr;
719 unsigned char save_control, save_freq_select;
720 unsigned int yrs;
721 #ifdef CONFIG_MACH_DECSTATION
722 unsigned int real_yrs;
723 #endif
724
725 if (!capable(CAP_SYS_TIME))
726 return -EACCES;
727
728 if (copy_from_user(&rtc_tm, (struct rtc_time __user *)arg,
729 sizeof(struct rtc_time)))
730 return -EFAULT;
731
732 yrs = rtc_tm.tm_year + 1900;
733 mon = rtc_tm.tm_mon + 1; /* tm_mon starts at zero */
734 day = rtc_tm.tm_mday;
735 hrs = rtc_tm.tm_hour;
736 min = rtc_tm.tm_min;
737 sec = rtc_tm.tm_sec;
738
739 if (yrs < 1970)
740 return -EINVAL;
741
742 leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
743
744 if ((mon > 12) || (day == 0))
745 return -EINVAL;
746
747 if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
748 return -EINVAL;
749
750 if ((hrs >= 24) || (min >= 60) || (sec >= 60))
751 return -EINVAL;
752
753 yrs -= epoch;
754 if (yrs > 255) /* They are unsigned */
755 return -EINVAL;
756
757 spin_lock_irq(&rtc_lock);
758 #ifdef CONFIG_MACH_DECSTATION
759 real_yrs = yrs;
760 yrs = 72;
761
762 /*
763 * We want to keep the year set to 73 until March
764 * for non-leap years, so that Feb, 29th is handled
765 * correctly.
766 */
767 if (!leap_yr && mon < 3) {
768 real_yrs--;
769 yrs = 73;
770 }
771 #endif
772 /* These limits and adjustments are independent of
773 * whether the chip is in binary mode or not.
774 */
775 if (yrs > 169) {
776 spin_unlock_irq(&rtc_lock);
777 return -EINVAL;
778 }
779 if (yrs >= 100)
780 yrs -= 100;
781
782 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY)
783 || RTC_ALWAYS_BCD) {
784 BIN_TO_BCD(sec);
785 BIN_TO_BCD(min);
786 BIN_TO_BCD(hrs);
787 BIN_TO_BCD(day);
788 BIN_TO_BCD(mon);
789 BIN_TO_BCD(yrs);
790 }
791
792 save_control = CMOS_READ(RTC_CONTROL);
793 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
794 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
795 CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
796
797 /*
798 * Make CMOS date writes nonpreemptible even on PREEMPT_RT.
799 * There's a limit to everything! =B-)
800 */
801 preempt_disable();
802 #ifdef CONFIG_MACH_DECSTATION
803 CMOS_WRITE(real_yrs, RTC_DEC_YEAR);
804 #endif
805 CMOS_WRITE(yrs, RTC_YEAR);
806 CMOS_WRITE(mon, RTC_MONTH);
807 CMOS_WRITE(day, RTC_DAY_OF_MONTH);
808 CMOS_WRITE(hrs, RTC_HOURS);
809 CMOS_WRITE(min, RTC_MINUTES);
810 CMOS_WRITE(sec, RTC_SECONDS);
811 preempt_enable();
812
813 CMOS_WRITE(save_control, RTC_CONTROL);
814 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
815
816 spin_unlock_irq(&rtc_lock);
817 return 0;
818 }
819 #ifdef RTC_IRQ
820 case RTC_IRQP_READ: /* Read the periodic IRQ rate. */
821 {
822 return put_user(rtc_freq, (unsigned long __user *)arg);
823 }
824 case RTC_IRQP_SET: /* Set periodic IRQ rate. */
825 {
826 int tmp = 0;
827 unsigned char val;
828 /* can be called from isr via rtc_control() */
829 unsigned long flags;
830
831 /*
832 * The max we can do is 8192Hz.
833 */
834 if ((arg < 2) || (arg > 8192))
835 return -EINVAL;
836 /*
837 * We don't really want Joe User generating more
838 * than 64Hz of interrupts on a multi-user machine.
839 */
840 if (!kernel && (arg > rtc_max_user_freq) &&
841 !capable(CAP_SYS_RESOURCE))
842 return -EACCES;
843
844 while (arg > (1<<tmp))
845 tmp++;
846
847 /*
848 * Check that the input was really a power of 2.
849 */
850 if (arg != (1<<tmp))
851 return -EINVAL;
852
853 spin_lock_irqsave(&rtc_lock, flags);
854 if (hpet_set_periodic_freq(arg)) {
855 spin_unlock_irqrestore(&rtc_lock, flags);
856 return 0;
857 }
858 rtc_freq = arg;
859
860 val = CMOS_READ(RTC_FREQ_SELECT) & 0xf0;
861 val |= (16 - tmp);
862 CMOS_WRITE(val, RTC_FREQ_SELECT);
863 spin_unlock_irqrestore(&rtc_lock, flags);
864 return 0;
865 }
866 #endif
867 case RTC_EPOCH_READ: /* Read the epoch. */
868 {
869 return put_user(epoch, (unsigned long __user *)arg);
870 }
871 case RTC_EPOCH_SET: /* Set the epoch. */
872 {
873 /*
874 * There were no RTC clocks before 1900.
875 */
876 if (arg < 1900)
877 return -EINVAL;
878
879 if (!capable(CAP_SYS_TIME))
880 return -EACCES;
881
882 epoch = arg;
883 return 0;
884 }
885 default:
886 return -ENOTTY;
887 }
888 return copy_to_user((void __user *)arg,
889 &wtime, sizeof wtime) ? -EFAULT : 0;
890 }
891
892 static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
893 unsigned long arg)
894 {
895 return rtc_do_ioctl(cmd, arg, 0);
896 }
897
898 /*
899 * We enforce only one user at a time here with the open/close.
900 * Also clear the previous interrupt data on an open, and clean
901 * up things on a close.
902 */
903
904 /* We use rtc_lock to protect against concurrent opens. So the BKL is not
905 * needed here. Or anywhere else in this driver. */
906 static int rtc_open(struct inode *inode, struct file *file)
907 {
908 spin_lock_irq(&rtc_lock);
909
910 if (rtc_status & RTC_IS_OPEN)
911 goto out_busy;
912
913 rtc_open_event();
914 rtc_status |= RTC_IS_OPEN;
915
916 rtc_irq_data = 0;
917 spin_unlock_irq(&rtc_lock);
918 return 0;
919
920 out_busy:
921 spin_unlock_irq(&rtc_lock);
922 return -EBUSY;
923 }
924
925 static int rtc_fasync(int fd, struct file *filp, int on)
926 {
927 return fasync_helper(fd, filp, on, &rtc_async_queue);
928 }
929
930 static int rtc_release(struct inode *inode, struct file *file)
931 {
932 #ifdef RTC_IRQ
933 unsigned char tmp;
934
935 if (rtc_has_irq == 0)
936 goto no_irq;
937
938 /*
939 * Turn off all interrupts once the device is no longer
940 * in use, and clear the data.
941 */
942
943 spin_lock_irq(&rtc_lock);
944 if (!hpet_mask_rtc_irq_bit(RTC_PIE | RTC_AIE | RTC_UIE)) {
945 tmp = CMOS_READ(RTC_CONTROL);
946 tmp &= ~RTC_PIE;
947 tmp &= ~RTC_AIE;
948 tmp &= ~RTC_UIE;
949 CMOS_WRITE(tmp, RTC_CONTROL);
950 CMOS_READ(RTC_INTR_FLAGS);
951 }
952 if (rtc_status & RTC_TIMER_ON) {
953 rtc_status &= ~RTC_TIMER_ON;
954 del_timer(&rtc_irq_timer);
955 }
956 spin_unlock_irq(&rtc_lock);
957
958 if (file->f_flags & FASYNC)
959 rtc_fasync(-1, file, 0);
960 no_irq:
961 #endif
962
963 spin_lock_irq(&rtc_lock);
964 rtc_irq_data = 0;
965 rtc_status &= ~RTC_IS_OPEN;
966 spin_unlock_irq(&rtc_lock);
967 rtc_close_event();
968
969 return 0;
970 }
971
972 #ifdef RTC_IRQ
973 /* Called without the kernel lock - fine */
974 static unsigned int rtc_poll(struct file *file, poll_table *wait)
975 {
976 unsigned long l;
977
978 if (rtc_has_irq == 0)
979 return 0;
980
981 poll_wait(file, &rtc_wait, wait);
982
983 spin_lock_irq(&rtc_lock);
984 l = rtc_irq_data;
985 spin_unlock_irq(&rtc_lock);
986
987 if (l != 0)
988 return POLLIN | POLLRDNORM;
989 return 0;
990 }
991 #endif
992
993 int rtc_register(rtc_task_t *task)
994 {
995 #ifndef RTC_IRQ
996 return -EIO;
997 #else
998 if (task == NULL || task->func == NULL)
999 return -EINVAL;
1000 spin_lock_irq(&rtc_lock);
1001 if (rtc_status & RTC_IS_OPEN) {
1002 spin_unlock_irq(&rtc_lock);
1003 return -EBUSY;
1004 }
1005 spin_lock(&rtc_task_lock);
1006 if (rtc_callback) {
1007 spin_unlock(&rtc_task_lock);
1008 spin_unlock_irq(&rtc_lock);
1009 return -EBUSY;
1010 }
1011 rtc_status |= RTC_IS_OPEN;
1012 rtc_callback = task;
1013 spin_unlock(&rtc_task_lock);
1014 spin_unlock_irq(&rtc_lock);
1015 return 0;
1016 #endif
1017 }
1018 EXPORT_SYMBOL(rtc_register);
1019
1020 int rtc_unregister(rtc_task_t *task)
1021 {
1022 #ifndef RTC_IRQ
1023 return -EIO;
1024 #else
1025 unsigned char tmp;
1026
1027 spin_lock_irq(&rtc_lock);
1028 spin_lock(&rtc_task_lock);
1029 if (rtc_callback != task) {
1030 spin_unlock(&rtc_task_lock);
1031 spin_unlock_irq(&rtc_lock);
1032 return -ENXIO;
1033 }
1034 rtc_callback = NULL;
1035
1036 /* disable controls */
1037 if (!hpet_mask_rtc_irq_bit(RTC_PIE | RTC_AIE | RTC_UIE)) {
1038 tmp = CMOS_READ(RTC_CONTROL);
1039 tmp &= ~RTC_PIE;
1040 tmp &= ~RTC_AIE;
1041 tmp &= ~RTC_UIE;
1042 CMOS_WRITE(tmp, RTC_CONTROL);
1043 CMOS_READ(RTC_INTR_FLAGS);
1044 }
1045 if (rtc_status & RTC_TIMER_ON) {
1046 rtc_status &= ~RTC_TIMER_ON;
1047 del_timer(&rtc_irq_timer);
1048 }
1049 rtc_status &= ~RTC_IS_OPEN;
1050 spin_unlock(&rtc_task_lock);
1051 spin_unlock_irq(&rtc_lock);
1052 return 0;
1053 #endif
1054 }
1055 EXPORT_SYMBOL(rtc_unregister);
1056
1057 int rtc_control(rtc_task_t *task, unsigned int cmd, unsigned long arg)
1058 {
1059 #ifndef RTC_IRQ
1060 return -EIO;
1061 #else
1062 unsigned long flags;
1063 if (cmd != RTC_PIE_ON && cmd != RTC_PIE_OFF && cmd != RTC_IRQP_SET)
1064 return -EINVAL;
1065 spin_lock_irqsave(&rtc_task_lock, flags);
1066 if (rtc_callback != task) {
1067 spin_unlock_irqrestore(&rtc_task_lock, flags);
1068 return -ENXIO;
1069 }
1070 spin_unlock_irqrestore(&rtc_task_lock, flags);
1071 return rtc_do_ioctl(cmd, arg, 1);
1072 #endif
1073 }
1074 EXPORT_SYMBOL(rtc_control);
1075
1076 /*
1077 * The various file operations we support.
1078 */
1079
1080 static const struct file_operations rtc_fops = {
1081 .owner = THIS_MODULE,
1082 .llseek = no_llseek,
1083 .read = rtc_read,
1084 #ifdef RTC_IRQ
1085 .poll = rtc_poll,
1086 #endif
1087 .ioctl = rtc_ioctl,
1088 .open = rtc_open,
1089 .release = rtc_release,
1090 .fasync = rtc_fasync,
1091 };
1092
1093 static struct miscdevice rtc_dev = {
1094 .minor = RTC_MINOR,
1095 .name = "rtc",
1096 .fops = &rtc_fops,
1097 };
1098
1099 #ifdef CONFIG_PROC_FS
1100 static const struct file_operations rtc_proc_fops = {
1101 .owner = THIS_MODULE,
1102 .open = rtc_proc_open,
1103 .read = seq_read,
1104 .llseek = seq_lseek,
1105 .release = single_release,
1106 };
1107 #endif
1108
1109 static resource_size_t rtc_size;
1110
1111 static struct resource * __init rtc_request_region(resource_size_t size)
1112 {
1113 struct resource *r;
1114
1115 if (RTC_IOMAPPED)
1116 r = request_region(RTC_PORT(0), size, "rtc");
1117 else
1118 r = request_mem_region(RTC_PORT(0), size, "rtc");
1119
1120 if (r)
1121 rtc_size = size;
1122
1123 return r;
1124 }
1125
1126 static void rtc_release_region(void)
1127 {
1128 if (RTC_IOMAPPED)
1129 release_region(RTC_PORT(0), rtc_size);
1130 else
1131 release_mem_region(RTC_PORT(0), rtc_size);
1132 }
1133
1134 static int __init rtc_init(void)
1135 {
1136 #ifdef CONFIG_PROC_FS
1137 struct proc_dir_entry *ent;
1138 #endif
1139 #if defined(__alpha__) || defined(__mips__)
1140 unsigned int year, ctrl;
1141 char *guess = NULL;
1142 #endif
1143 #ifdef CONFIG_SPARC32
1144 struct linux_ebus *ebus;
1145 struct linux_ebus_device *edev;
1146 #else
1147 void *r;
1148 #ifdef RTC_IRQ
1149 irq_handler_t rtc_int_handler_ptr;
1150 #endif
1151 #endif
1152
1153 #ifdef CONFIG_SPARC32
1154 for_each_ebus(ebus) {
1155 for_each_ebusdev(edev, ebus) {
1156 if (strcmp(edev->prom_node->name, "rtc") == 0) {
1157 rtc_port = edev->resource[0].start;
1158 rtc_irq = edev->irqs[0];
1159 goto found;
1160 }
1161 }
1162 }
1163 rtc_has_irq = 0;
1164 printk(KERN_ERR "rtc_init: no PC rtc found\n");
1165 return -EIO;
1166
1167 found:
1168 if (rtc_irq == PCI_IRQ_NONE) {
1169 rtc_has_irq = 0;
1170 goto no_irq;
1171 }
1172
1173 /*
1174 * XXX Interrupt pin #7 in Espresso is shared between RTC and
1175 * PCI Slot 2 INTA# (and some INTx# in Slot 1).
1176 */
1177 if (request_irq(rtc_irq, rtc_interrupt, IRQF_SHARED, "rtc",
1178 (void *)&rtc_port)) {
1179 rtc_has_irq = 0;
1180 printk(KERN_ERR "rtc: cannot register IRQ %d\n", rtc_irq);
1181 return -EIO;
1182 }
1183 no_irq:
1184 #else
1185 r = rtc_request_region(RTC_IO_EXTENT);
1186
1187 /*
1188 * If we've already requested a smaller range (for example, because
1189 * PNPBIOS or ACPI told us how the device is configured), the request
1190 * above might fail because it's too big.
1191 *
1192 * If so, request just the range we actually use.
1193 */
1194 if (!r)
1195 r = rtc_request_region(RTC_IO_EXTENT_USED);
1196 if (!r) {
1197 #ifdef RTC_IRQ
1198 rtc_has_irq = 0;
1199 #endif
1200 printk(KERN_ERR "rtc: I/O resource %lx is not free.\n",
1201 (long)(RTC_PORT(0)));
1202 return -EIO;
1203 }
1204
1205 #ifdef RTC_IRQ
1206 if (is_hpet_enabled()) {
1207 int err;
1208
1209 rtc_int_handler_ptr = hpet_rtc_interrupt;
1210 err = hpet_register_irq_handler(rtc_interrupt);
1211 if (err != 0) {
1212 printk(KERN_WARNING "hpet_register_irq_handler failed "
1213 "in rtc_init().");
1214 return err;
1215 }
1216 } else {
1217 rtc_int_handler_ptr = rtc_interrupt;
1218 }
1219
1220 if (request_irq(RTC_IRQ, rtc_int_handler_ptr, IRQF_DISABLED,
1221 "rtc", NULL)) {
1222 /* Yeah right, seeing as irq 8 doesn't even hit the bus. */
1223 rtc_has_irq = 0;
1224 printk(KERN_ERR "rtc: IRQ %d is not free.\n", RTC_IRQ);
1225 rtc_release_region();
1226
1227 return -EIO;
1228 }
1229 hpet_rtc_timer_init();
1230
1231 #endif
1232
1233 #endif /* CONFIG_SPARC32 vs. others */
1234
1235 if (misc_register(&rtc_dev)) {
1236 #ifdef RTC_IRQ
1237 free_irq(RTC_IRQ, NULL);
1238 hpet_unregister_irq_handler(rtc_interrupt);
1239 rtc_has_irq = 0;
1240 #endif
1241 rtc_release_region();
1242 return -ENODEV;
1243 }
1244
1245 #ifdef CONFIG_PROC_FS
1246 ent = create_proc_entry("driver/rtc", 0, NULL);
1247 if (ent)
1248 ent->proc_fops = &rtc_proc_fops;
1249 else
1250 printk(KERN_WARNING "rtc: Failed to register with procfs.\n");
1251 #endif
1252
1253 #if defined(__alpha__) || defined(__mips__)
1254 rtc_freq = HZ;
1255
1256 /* Each operating system on an Alpha uses its own epoch.
1257 Let's try to guess which one we are using now. */
1258
1259 if (rtc_is_updating() != 0)
1260 msleep(20);
1261
1262 spin_lock_irq(&rtc_lock);
1263 year = CMOS_READ(RTC_YEAR);
1264 ctrl = CMOS_READ(RTC_CONTROL);
1265 spin_unlock_irq(&rtc_lock);
1266
1267 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
1268 BCD_TO_BIN(year); /* This should never happen... */
1269
1270 if (year < 20) {
1271 epoch = 2000;
1272 guess = "SRM (post-2000)";
1273 } else if (year >= 20 && year < 48) {
1274 epoch = 1980;
1275 guess = "ARC console";
1276 } else if (year >= 48 && year < 72) {
1277 epoch = 1952;
1278 guess = "Digital UNIX";
1279 #if defined(__mips__)
1280 } else if (year >= 72 && year < 74) {
1281 epoch = 2000;
1282 guess = "Digital DECstation";
1283 #else
1284 } else if (year >= 70) {
1285 epoch = 1900;
1286 guess = "Standard PC (1900)";
1287 #endif
1288 }
1289 if (guess)
1290 printk(KERN_INFO "rtc: %s epoch (%lu) detected\n",
1291 guess, epoch);
1292 #endif
1293 #ifdef RTC_IRQ
1294 if (rtc_has_irq == 0)
1295 goto no_irq2;
1296
1297 spin_lock_irq(&rtc_lock);
1298 rtc_freq = 1024;
1299 if (!hpet_set_periodic_freq(rtc_freq)) {
1300 /*
1301 * Initialize periodic frequency to CMOS reset default,
1302 * which is 1024Hz
1303 */
1304 CMOS_WRITE(((CMOS_READ(RTC_FREQ_SELECT) & 0xF0) | 0x06),
1305 RTC_FREQ_SELECT);
1306 }
1307 spin_unlock_irq(&rtc_lock);
1308 no_irq2:
1309 #endif
1310
1311 (void) init_sysctl();
1312
1313 printk(KERN_INFO "Real Time Clock Driver v" RTC_VERSION "\n");
1314
1315 return 0;
1316 }
1317
1318 static void __exit rtc_exit(void)
1319 {
1320 cleanup_sysctl();
1321 remove_proc_entry("driver/rtc", NULL);
1322 misc_deregister(&rtc_dev);
1323
1324 #ifdef CONFIG_SPARC32
1325 if (rtc_has_irq)
1326 free_irq(rtc_irq, &rtc_port);
1327 #else
1328 rtc_release_region();
1329 #ifdef RTC_IRQ
1330 if (rtc_has_irq) {
1331 free_irq(RTC_IRQ, NULL);
1332 hpet_unregister_irq_handler(hpet_rtc_interrupt);
1333 }
1334 #endif
1335 #endif /* CONFIG_SPARC32 */
1336 }
1337
1338 module_init(rtc_init);
1339 module_exit(rtc_exit);
1340
1341 #ifdef RTC_IRQ
1342 /*
1343 * At IRQ rates >= 4096Hz, an interrupt may get lost altogether.
1344 * (usually during an IDE disk interrupt, with IRQ unmasking off)
1345 * Since the interrupt handler doesn't get called, the IRQ status
1346 * byte doesn't get read, and the RTC stops generating interrupts.
1347 * A timer is set, and will call this function if/when that happens.
1348 * To get it out of this stalled state, we just read the status.
1349 * At least a jiffy of interrupts (rtc_freq/HZ) will have been lost.
1350 * (You *really* shouldn't be trying to use a non-realtime system
1351 * for something that requires a steady > 1KHz signal anyways.)
1352 */
1353
1354 static void rtc_dropped_irq(unsigned long data)
1355 {
1356 unsigned long freq;
1357
1358 spin_lock_irq(&rtc_lock);
1359
1360 if (hpet_rtc_dropped_irq()) {
1361 spin_unlock_irq(&rtc_lock);
1362 return;
1363 }
1364
1365 /* Just in case someone disabled the timer from behind our back... */
1366 if (rtc_status & RTC_TIMER_ON)
1367 mod_timer(&rtc_irq_timer, jiffies + HZ/rtc_freq + 2*HZ/100);
1368
1369 rtc_irq_data += ((rtc_freq/HZ)<<8);
1370 rtc_irq_data &= ~0xff;
1371 rtc_irq_data |= (CMOS_READ(RTC_INTR_FLAGS) & 0xF0); /* restart */
1372
1373 freq = rtc_freq;
1374
1375 spin_unlock_irq(&rtc_lock);
1376
1377 #ifndef CONFIG_PREEMPT_RT
1378 if (printk_ratelimit()) {
1379 printk(KERN_WARNING "rtc: lost some interrupts at %ldHz.\n",
1380 freq);
1381 }
1382 #endif
1383
1384 /* Now we have new data */
1385 wake_up_interruptible(&rtc_wait);
1386
1387 kill_fasync(&rtc_async_queue, SIGIO, POLL_IN);
1388 }
1389 #endif
1390
1391 #ifdef CONFIG_PROC_FS
1392 /*
1393 * Info exported via "/proc/driver/rtc".
1394 */
1395
1396 static int rtc_proc_show(struct seq_file *seq, void *v)
1397 {
1398 #define YN(bit) ((ctrl & bit) ? "yes" : "no")
1399 #define NY(bit) ((ctrl & bit) ? "no" : "yes")
1400 struct rtc_time tm;
1401 unsigned char batt, ctrl;
1402 unsigned long freq;
1403
1404 spin_lock_irq(&rtc_lock);
1405 batt = CMOS_READ(RTC_VALID) & RTC_VRT;
1406 ctrl = CMOS_READ(RTC_CONTROL);
1407 freq = rtc_freq;
1408 spin_unlock_irq(&rtc_lock);
1409
1410
1411 rtc_get_rtc_time(&tm);
1412
1413 /*
1414 * There is no way to tell if the luser has the RTC set for local
1415 * time or for Universal Standard Time (GMT). Probably local though.
1416 */
1417 seq_printf(seq,
1418 "rtc_time\t: %02d:%02d:%02d\n"
1419 "rtc_date\t: %04d-%02d-%02d\n"
1420 "rtc_epoch\t: %04lu\n",
1421 tm.tm_hour, tm.tm_min, tm.tm_sec,
1422 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, epoch);
1423
1424 get_rtc_alm_time(&tm);
1425
1426 /*
1427 * We implicitly assume 24hr mode here. Alarm values >= 0xc0 will
1428 * match any value for that particular field. Values that are
1429 * greater than a valid time, but less than 0xc0 shouldn't appear.
1430 */
1431 seq_puts(seq, "alarm\t\t: ");
1432 if (tm.tm_hour <= 24)
1433 seq_printf(seq, "%02d:", tm.tm_hour);
1434 else
1435 seq_puts(seq, "**:");
1436
1437 if (tm.tm_min <= 59)
1438 seq_printf(seq, "%02d:", tm.tm_min);
1439 else
1440 seq_puts(seq, "**:");
1441
1442 if (tm.tm_sec <= 59)
1443 seq_printf(seq, "%02d\n", tm.tm_sec);
1444 else
1445 seq_puts(seq, "**\n");
1446
1447 seq_printf(seq,
1448 "DST_enable\t: %s\n"
1449 "BCD\t\t: %s\n"
1450 "24hr\t\t: %s\n"
1451 "square_wave\t: %s\n"
1452 "alarm_IRQ\t: %s\n"
1453 "update_IRQ\t: %s\n"
1454 "periodic_IRQ\t: %s\n"
1455 "periodic_freq\t: %ld\n"
1456 "batt_status\t: %s\n",
1457 YN(RTC_DST_EN),
1458 NY(RTC_DM_BINARY),
1459 YN(RTC_24H),
1460 YN(RTC_SQWE),
1461 YN(RTC_AIE),
1462 YN(RTC_UIE),
1463 YN(RTC_PIE),
1464 freq,
1465 batt ? "okay" : "dead");
1466
1467 return 0;
1468 #undef YN
1469 #undef NY
1470 }
1471
1472 static int rtc_proc_open(struct inode *inode, struct file *file)
1473 {
1474 return single_open(file, rtc_proc_show, NULL);
1475 }
1476 #endif
1477
1478 void rtc_get_rtc_time(struct rtc_time *rtc_tm)
1479 {
1480 unsigned long uip_watchdog = jiffies, flags;
1481 unsigned char ctrl;
1482 #ifdef CONFIG_MACH_DECSTATION
1483 unsigned int real_year;
1484 #endif
1485
1486 /*
1487 * read RTC once any update in progress is done. The update
1488 * can take just over 2ms. We wait 20ms. There is no need to
1489 * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
1490 * If you need to know *exactly* when a second has started, enable
1491 * periodic update complete interrupts, (via ioctl) and then
1492 * immediately read /dev/rtc which will block until you get the IRQ.
1493 * Once the read clears, read the RTC time (again via ioctl). Easy.
1494 */
1495
1496 while (rtc_is_updating() != 0 && jiffies - uip_watchdog < 2*HZ/100)
1497 cpu_relax();
1498
1499 /*
1500 * Only the values that we read from the RTC are set. We leave
1501 * tm_wday, tm_yday and tm_isdst untouched. Note that while the
1502 * RTC has RTC_DAY_OF_WEEK, we should usually ignore it, as it is
1503 * only updated by the RTC when initially set to a non-zero value.
1504 */
1505 spin_lock_irqsave(&rtc_lock, flags);
1506 rtc_tm->tm_sec = CMOS_READ(RTC_SECONDS);
1507 rtc_tm->tm_min = CMOS_READ(RTC_MINUTES);
1508 rtc_tm->tm_hour = CMOS_READ(RTC_HOURS);
1509 rtc_tm->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH);
1510 rtc_tm->tm_mon = CMOS_READ(RTC_MONTH);
1511 rtc_tm->tm_year = CMOS_READ(RTC_YEAR);
1512 /* Only set from 2.6.16 onwards */
1513 rtc_tm->tm_wday = CMOS_READ(RTC_DAY_OF_WEEK);
1514
1515 #ifdef CONFIG_MACH_DECSTATION
1516 real_year = CMOS_READ(RTC_DEC_YEAR);
1517 #endif
1518 ctrl = CMOS_READ(RTC_CONTROL);
1519 spin_unlock_irqrestore(&rtc_lock, flags);
1520
1521 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
1522 BCD_TO_BIN(rtc_tm->tm_sec);
1523 BCD_TO_BIN(rtc_tm->tm_min);
1524 BCD_TO_BIN(rtc_tm->tm_hour);
1525 BCD_TO_BIN(rtc_tm->tm_mday);
1526 BCD_TO_BIN(rtc_tm->tm_mon);
1527 BCD_TO_BIN(rtc_tm->tm_year);
1528 BCD_TO_BIN(rtc_tm->tm_wday);
1529 }
1530
1531 #ifdef CONFIG_MACH_DECSTATION
1532 rtc_tm->tm_year += real_year - 72;
1533 #endif
1534
1535 /*
1536 * Account for differences between how the RTC uses the values
1537 * and how they are defined in a struct rtc_time;
1538 */
1539 rtc_tm->tm_year += epoch - 1900;
1540 if (rtc_tm->tm_year <= 69)
1541 rtc_tm->tm_year += 100;
1542
1543 rtc_tm->tm_mon--;
1544 }
1545
1546 static void get_rtc_alm_time(struct rtc_time *alm_tm)
1547 {
1548 unsigned char ctrl;
1549
1550 /*
1551 * Only the values that we read from the RTC are set. That
1552 * means only tm_hour, tm_min, and tm_sec.
1553 */
1554 spin_lock_irq(&rtc_lock);
1555 alm_tm->tm_sec = CMOS_READ(RTC_SECONDS_ALARM);
1556 alm_tm->tm_min = CMOS_READ(RTC_MINUTES_ALARM);
1557 alm_tm->tm_hour = CMOS_READ(RTC_HOURS_ALARM);
1558 ctrl = CMOS_READ(RTC_CONTROL);
1559 spin_unlock_irq(&rtc_lock);
1560
1561 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
1562 BCD_TO_BIN(alm_tm->tm_sec);
1563 BCD_TO_BIN(alm_tm->tm_min);
1564 BCD_TO_BIN(alm_tm->tm_hour);
1565 }
1566 }
1567
1568 #ifdef RTC_IRQ
1569 /*
1570 * Used to disable/enable interrupts for any one of UIE, AIE, PIE.
1571 * Rumour has it that if you frob the interrupt enable/disable
1572 * bits in RTC_CONTROL, you should read RTC_INTR_FLAGS, to
1573 * ensure you actually start getting interrupts. Probably for
1574 * compatibility with older/broken chipset RTC implementations.
1575 * We also clear out any old irq data after an ioctl() that
1576 * meddles with the interrupt enable/disable bits.
1577 */
1578
1579 static void mask_rtc_irq_bit_locked(unsigned char bit)
1580 {
1581 unsigned char val;
1582
1583 if (hpet_mask_rtc_irq_bit(bit))
1584 return;
1585 val = CMOS_READ(RTC_CONTROL);
1586 val &= ~bit;
1587 CMOS_WRITE(val, RTC_CONTROL);
1588 CMOS_READ(RTC_INTR_FLAGS);
1589
1590 rtc_irq_data = 0;
1591 }
1592
1593 static void set_rtc_irq_bit_locked(unsigned char bit)
1594 {
1595 unsigned char val;
1596
1597 if (hpet_set_rtc_irq_bit(bit))
1598 return;
1599 val = CMOS_READ(RTC_CONTROL);
1600 val |= bit;
1601 CMOS_WRITE(val, RTC_CONTROL);
1602 CMOS_READ(RTC_INTR_FLAGS);
1603
1604 rtc_irq_data = 0;
1605 }
1606 #endif
1607
1608 MODULE_AUTHOR("Paul Gortmaker");
1609 MODULE_LICENSE("GPL");
1610 MODULE_ALIAS_MISCDEV(RTC_MINOR);
1611
|
This page was automatically generated by the
LXR engine.
|