1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47#define RTC_VERSION "1.10f"
48
49
50
51
52
53
54
55
56
57#include <linux/config.h>
58#include <linux/module.h>
59#include <linux/kernel.h>
60#include <linux/types.h>
61#include <linux/miscdevice.h>
62#include <linux/ioport.h>
63#include <linux/fcntl.h>
64#include <linux/mc146818rtc.h>
65#include <linux/init.h>
66#include <linux/poll.h>
67#include <linux/proc_fs.h>
68#include <linux/spinlock.h>
69#include <linux/sysctl.h>
70
71#include <asm/io.h>
72#include <asm/uaccess.h>
73#include <asm/system.h>
74
75#ifdef __sparc__
76#include <linux/pci.h>
77#include <asm/ebus.h>
78#ifdef __sparc_v9__
79#include <asm/isa.h>
80#endif
81
82static unsigned long rtc_port;
83static int rtc_irq = PCI_IRQ_NONE;
84#endif
85
86#if RTC_IRQ
87static int rtc_has_irq = 1;
88#endif
89
90
91
92
93
94
95
96
97static struct fasync_struct *rtc_async_queue;
98
99static DECLARE_WAIT_QUEUE_HEAD(rtc_wait);
100
101#if RTC_IRQ
102static struct timer_list rtc_irq_timer;
103#endif
104
105static ssize_t rtc_read(struct file *file, char *buf,
106 size_t count, loff_t *ppos);
107
108static int rtc_ioctl(struct inode *inode, struct file *file,
109 unsigned int cmd, unsigned long arg);
110
111#if RTC_IRQ
112static unsigned int rtc_poll(struct file *file, poll_table *wait);
113#endif
114
115static void get_rtc_time (struct rtc_time *rtc_tm);
116static void get_rtc_alm_time (struct rtc_time *alm_tm);
117#if RTC_IRQ
118static void rtc_dropped_irq(unsigned long data);
119
120static void set_rtc_irq_bit(unsigned char bit);
121static void mask_rtc_irq_bit(unsigned char bit);
122#endif
123
124static inline unsigned char rtc_is_updating(void);
125
126static int rtc_read_proc(char *page, char **start, off_t off,
127 int count, int *eof, void *data);
128
129
130
131
132
133#define RTC_IS_OPEN 0x01
134#define RTC_TIMER_ON 0x02
135
136
137
138
139
140
141
142
143static unsigned long rtc_status = 0;
144static unsigned long rtc_freq = 0;
145static unsigned long rtc_irq_data = 0;
146static unsigned long rtc_max_user_freq = 64;
147
148
149
150
151
152
153static unsigned long epoch = 1900;
154
155static const unsigned char days_in_mo[] =
156{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
157
158#if RTC_IRQ
159
160
161
162
163
164
165
166
167
168
169static void rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
170{
171
172
173
174
175
176
177
178 spin_lock (&rtc_lock);
179 rtc_irq_data += 0x100;
180 rtc_irq_data &= ~0xff;
181 rtc_irq_data |= (CMOS_READ(RTC_INTR_FLAGS) & 0xF0);
182
183 if (rtc_status & RTC_TIMER_ON)
184 mod_timer(&rtc_irq_timer, jiffies + HZ/rtc_freq + 2*HZ/100);
185
186 spin_unlock (&rtc_lock);
187
188
189 wake_up_interruptible(&rtc_wait);
190
191 kill_fasync (&rtc_async_queue, SIGIO, POLL_IN);
192}
193#endif
194
195
196
197
198static ctl_table rtc_table[] = {
199 { 1, "max-user-freq", &rtc_max_user_freq, sizeof(int), 0644, NULL,
200 &proc_dointvec, NULL, },
201 { 0, }
202};
203
204static ctl_table rtc_root[] = {
205 { 1, "rtc", NULL, 0, 0555, rtc_table, },
206 { 0, }
207};
208
209static ctl_table dev_root[] = {
210 { CTL_DEV, "dev", NULL, 0, 0555, rtc_root, },
211 { 0, }
212};
213
214static struct ctl_table_header *sysctl_header;
215
216static int __init init_sysctl(void)
217{
218 sysctl_header = register_sysctl_table(dev_root, 0);
219 return 0;
220}
221
222static void __exit cleanup_sysctl(void)
223{
224 unregister_sysctl_table(sysctl_header);
225}
226
227
228
229
230
231static ssize_t rtc_read(struct file *file, char *buf,
232 size_t count, loff_t *ppos)
233{
234#if !RTC_IRQ
235 return -EIO;
236#else
237 DECLARE_WAITQUEUE(wait, current);
238 unsigned long data;
239 ssize_t retval;
240
241 if (rtc_has_irq == 0)
242 return -EIO;
243
244
245
246
247
248
249
250
251
252 if (count != sizeof(unsigned int) && count != sizeof(unsigned long))
253 return -EINVAL;
254
255 add_wait_queue(&rtc_wait, &wait);
256
257 do {
258 __set_current_state(TASK_INTERRUPTIBLE);
259
260
261
262
263 spin_lock_irq (&rtc_lock);
264 data = rtc_irq_data;
265 rtc_irq_data = 0;
266 spin_unlock_irq (&rtc_lock);
267
268 if (data != 0)
269 break;
270
271 if (file->f_flags & O_NONBLOCK) {
272 retval = -EAGAIN;
273 goto out;
274 }
275 if (signal_pending(current)) {
276 retval = -ERESTARTSYS;
277 goto out;
278 }
279 schedule();
280 } while (1);
281
282 if (count == sizeof(unsigned int))
283 retval = put_user(data, (unsigned int *)buf);
284 else
285 retval = put_user(data, (unsigned long *)buf);
286 if (!retval)
287 retval = count;
288 out:
289 current->state = TASK_RUNNING;
290 remove_wait_queue(&rtc_wait, &wait);
291
292 return retval;
293#endif
294}
295
296static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
297 unsigned long arg)
298{
299 struct rtc_time wtime;
300
301#if RTC_IRQ
302 if (rtc_has_irq == 0) {
303 switch (cmd) {
304 case RTC_AIE_OFF:
305 case RTC_AIE_ON:
306 case RTC_PIE_OFF:
307 case RTC_PIE_ON:
308 case RTC_UIE_OFF:
309 case RTC_UIE_ON:
310 case RTC_IRQP_READ:
311 case RTC_IRQP_SET:
312 return -EINVAL;
313 };
314 }
315#endif
316
317 switch (cmd) {
318#if RTC_IRQ
319 case RTC_AIE_OFF:
320 {
321 mask_rtc_irq_bit(RTC_AIE);
322 return 0;
323 }
324 case RTC_AIE_ON:
325 {
326 set_rtc_irq_bit(RTC_AIE);
327 return 0;
328 }
329 case RTC_PIE_OFF:
330 {
331 mask_rtc_irq_bit(RTC_PIE);
332 if (rtc_status & RTC_TIMER_ON) {
333 spin_lock_irq (&rtc_lock);
334 rtc_status &= ~RTC_TIMER_ON;
335 del_timer(&rtc_irq_timer);
336 spin_unlock_irq (&rtc_lock);
337 }
338 return 0;
339 }
340 case RTC_PIE_ON:
341 {
342
343
344
345
346
347 if ((rtc_freq > rtc_max_user_freq) &&
348 (!capable(CAP_SYS_RESOURCE)))
349 return -EACCES;
350
351 if (!(rtc_status & RTC_TIMER_ON)) {
352 spin_lock_irq (&rtc_lock);
353 rtc_irq_timer.expires = jiffies + HZ/rtc_freq + 2*HZ/100;
354 add_timer(&rtc_irq_timer);
355 rtc_status |= RTC_TIMER_ON;
356 spin_unlock_irq (&rtc_lock);
357 }
358 set_rtc_irq_bit(RTC_PIE);
359 return 0;
360 }
361 case RTC_UIE_OFF:
362 {
363 mask_rtc_irq_bit(RTC_UIE);
364 return 0;
365 }
366 case RTC_UIE_ON:
367 {
368 set_rtc_irq_bit(RTC_UIE);
369 return 0;
370 }
371#endif
372 case RTC_ALM_READ:
373 {
374
375
376
377
378
379 memset(&wtime, 0, sizeof(struct rtc_time));
380 get_rtc_alm_time(&wtime);
381 break;
382 }
383 case RTC_ALM_SET:
384 {
385
386
387
388
389
390 unsigned char hrs, min, sec;
391 struct rtc_time alm_tm;
392
393 if (copy_from_user(&alm_tm, (struct rtc_time*)arg,
394 sizeof(struct rtc_time)))
395 return -EFAULT;
396
397 hrs = alm_tm.tm_hour;
398 min = alm_tm.tm_min;
399 sec = alm_tm.tm_sec;
400
401 spin_lock_irq(&rtc_lock);
402 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) ||
403 RTC_ALWAYS_BCD)
404 {
405 if (sec < 60) BIN_TO_BCD(sec);
406 else sec = 0xff;
407
408 if (min < 60) BIN_TO_BCD(min);
409 else min = 0xff;
410
411 if (hrs < 24) BIN_TO_BCD(hrs);
412 else hrs = 0xff;
413 }
414 CMOS_WRITE(hrs, RTC_HOURS_ALARM);
415 CMOS_WRITE(min, RTC_MINUTES_ALARM);
416 CMOS_WRITE(sec, RTC_SECONDS_ALARM);
417 spin_unlock_irq(&rtc_lock);
418
419 return 0;
420 }
421 case RTC_RD_TIME:
422 {
423 memset(&wtime, 0, sizeof(struct rtc_time));
424 get_rtc_time(&wtime);
425 break;
426 }
427 case RTC_SET_TIME:
428 {
429 struct rtc_time rtc_tm;
430 unsigned char mon, day, hrs, min, sec, leap_yr;
431 unsigned char save_control, save_freq_select;
432 unsigned int yrs;
433#ifdef CONFIG_DECSTATION
434 unsigned int real_yrs;
435#endif
436
437 if (!capable(CAP_SYS_TIME))
438 return -EACCES;
439
440 if (copy_from_user(&rtc_tm, (struct rtc_time*)arg,
441 sizeof(struct rtc_time)))
442 return -EFAULT;
443
444 yrs = rtc_tm.tm_year + 1900;
445 mon = rtc_tm.tm_mon + 1;
446 day = rtc_tm.tm_mday;
447 hrs = rtc_tm.tm_hour;
448 min = rtc_tm.tm_min;
449 sec = rtc_tm.tm_sec;
450
451 if (yrs < 1970)
452 return -EINVAL;
453
454 leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
455
456 if ((mon > 12) || (day == 0))
457 return -EINVAL;
458
459 if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
460 return -EINVAL;
461
462 if ((hrs >= 24) || (min >= 60) || (sec >= 60))
463 return -EINVAL;
464
465 if ((yrs -= epoch) > 255)
466 return -EINVAL;
467
468 spin_lock_irq(&rtc_lock);
469#ifdef CONFIG_DECSTATION
470 real_yrs = yrs;
471 yrs = 72;
472
473
474
475
476
477
478 if (!leap_yr && mon < 3) {
479 real_yrs--;
480 yrs = 73;
481 }
482#endif
483
484
485
486 if (yrs > 169) {
487 spin_unlock_irq(&rtc_lock);
488 return -EINVAL;
489 }
490 if (yrs >= 100)
491 yrs -= 100;
492
493 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY)
494 || RTC_ALWAYS_BCD) {
495 BIN_TO_BCD(sec);
496 BIN_TO_BCD(min);
497 BIN_TO_BCD(hrs);
498 BIN_TO_BCD(day);
499 BIN_TO_BCD(mon);
500 BIN_TO_BCD(yrs);
501 }
502
503 save_control = CMOS_READ(RTC_CONTROL);
504 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
505 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
506 CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
507
508#ifdef CONFIG_DECSTATION
509 CMOS_WRITE(real_yrs, RTC_DEC_YEAR);
510#endif
511 CMOS_WRITE(yrs, RTC_YEAR);
512 CMOS_WRITE(mon, RTC_MONTH);
513 CMOS_WRITE(day, RTC_DAY_OF_MONTH);
514 CMOS_WRITE(hrs, RTC_HOURS);
515 CMOS_WRITE(min, RTC_MINUTES);
516 CMOS_WRITE(sec, RTC_SECONDS);
517
518 CMOS_WRITE(save_control, RTC_CONTROL);
519 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
520
521 spin_unlock_irq(&rtc_lock);
522 return 0;
523 }
524#if RTC_IRQ
525 case RTC_IRQP_READ:
526 {
527 return put_user(rtc_freq, (unsigned long *)arg);
528 }
529 case RTC_IRQP_SET:
530 {
531 int tmp = 0;
532 unsigned char val;
533
534
535
536
537 if ((arg < 2) || (arg > 8192))
538 return -EINVAL;
539
540
541
542
543 if ((arg > rtc_max_user_freq) && (!capable(CAP_SYS_RESOURCE)))
544 return -EACCES;
545
546 while (arg > (1<<tmp))
547 tmp++;
548
549
550
551
552 if (arg != (1<<tmp))
553 return -EINVAL;
554
555 spin_lock_irq(&rtc_lock);
556 rtc_freq = arg;
557
558 val = CMOS_READ(RTC_FREQ_SELECT) & 0xf0;
559 val |= (16 - tmp);
560 CMOS_WRITE(val, RTC_FREQ_SELECT);
561 spin_unlock_irq(&rtc_lock);
562 return 0;
563 }
564#endif
565 case RTC_EPOCH_READ:
566 {
567 return put_user (epoch, (unsigned long *)arg);
568 }
569 case RTC_EPOCH_SET:
570 {
571
572
573
574 if (arg < 1900)
575 return -EINVAL;
576
577 if (!capable(CAP_SYS_TIME))
578 return -EACCES;
579
580 epoch = arg;
581 return 0;
582 }
583 default:
584 return -ENOTTY;
585 }
586 return copy_to_user((void *)arg, &wtime, sizeof wtime) ? -EFAULT : 0;
587}
588
589
590
591
592
593
594
595
596
597static int rtc_open(struct inode *inode, struct file *file)
598{
599 spin_lock_irq (&rtc_lock);
600
601 if(rtc_status & RTC_IS_OPEN)
602 goto out_busy;
603
604 rtc_status |= RTC_IS_OPEN;
605
606 rtc_irq_data = 0;
607 spin_unlock_irq (&rtc_lock);
608 return 0;
609
610out_busy:
611 spin_unlock_irq (&rtc_lock);
612 return -EBUSY;
613}
614
615static int rtc_fasync (int fd, struct file *filp, int on)
616
617{
618 return fasync_helper (fd, filp, on, &rtc_async_queue);
619}
620
621static int rtc_release(struct inode *inode, struct file *file)
622{
623#if RTC_IRQ
624 unsigned char tmp;
625
626 if (rtc_has_irq == 0)
627 goto no_irq;
628
629
630
631
632
633
634 spin_lock_irq(&rtc_lock);
635 tmp = CMOS_READ(RTC_CONTROL);
636 tmp &= ~RTC_PIE;
637 tmp &= ~RTC_AIE;
638 tmp &= ~RTC_UIE;
639 CMOS_WRITE(tmp, RTC_CONTROL);
640 CMOS_READ(RTC_INTR_FLAGS);
641
642 if (rtc_status & RTC_TIMER_ON) {
643 rtc_status &= ~RTC_TIMER_ON;
644 del_timer(&rtc_irq_timer);
645 }
646 spin_unlock_irq(&rtc_lock);
647
648 if (file->f_flags & FASYNC) {
649 rtc_fasync (-1, file, 0);
650 }
651no_irq:
652#endif
653
654 spin_lock_irq (&rtc_lock);
655 rtc_irq_data = 0;
656 spin_unlock_irq (&rtc_lock);
657
658
659
660 rtc_status &= ~RTC_IS_OPEN;
661 return 0;
662}
663
664#if RTC_IRQ
665
666static unsigned int rtc_poll(struct file *file, poll_table *wait)
667{
668 unsigned long l;
669
670 if (rtc_has_irq == 0)
671 return 0;
672
673 poll_wait(file, &rtc_wait, wait);
674
675 spin_lock_irq (&rtc_lock);
676 l = rtc_irq_data;
677 spin_unlock_irq (&rtc_lock);
678
679 if (l != 0)
680 return POLLIN | POLLRDNORM;
681 return 0;
682}
683#endif
684
685
686
687
688
689static struct file_operations rtc_fops = {
690 owner: THIS_MODULE,
691 llseek: no_llseek,
692 read: rtc_read,
693#if RTC_IRQ
694 poll: rtc_poll,
695#endif
696 ioctl: rtc_ioctl,
697 open: rtc_open,
698 release: rtc_release,
699 fasync: rtc_fasync,
700};
701
702static struct miscdevice rtc_dev=
703{
704 RTC_MINOR,
705 "rtc",
706 &rtc_fops
707};
708
709static int __init rtc_init(void)
710{
711#if defined(__alpha__) || defined(__mips__)
712 unsigned int year, ctrl;
713 unsigned long uip_watchdog;
714 char *guess = NULL;
715#endif
716#ifdef __sparc__
717 struct linux_ebus *ebus;
718 struct linux_ebus_device *edev;
719#ifdef __sparc_v9__
720 struct isa_bridge *isa_br;
721 struct isa_device *isa_dev;
722#endif
723#endif
724#ifndef __sparc__
725 void *r;
726#endif
727
728#ifdef __sparc__
729 for_each_ebus(ebus) {
730 for_each_ebusdev(edev, ebus) {
731 if(strcmp(edev->prom_name, "rtc") == 0) {
732 rtc_port = edev->resource[0].start;
733 rtc_irq = edev->irqs[0];
734 goto found;
735 }
736 }
737 }
738#ifdef __sparc_v9__
739 for_each_isa(isa_br) {
740 for_each_isadev(isa_dev, isa_br) {
741 if (strcmp(isa_dev->prom_name, "rtc") == 0) {
742 rtc_port = isa_dev->resource.start;
743 rtc_irq = isa_dev->irq;
744 goto found;
745 }
746 }
747 }
748#endif
749 printk(KERN_ERR "rtc_init: no PC rtc found\n");
750 return -EIO;
751
752found:
753 if (rtc_irq == PCI_IRQ_NONE) {
754 rtc_has_irq = 0;
755 goto no_irq;
756 }
757
758
759
760
761
762
763 if (request_irq(rtc_irq, rtc_interrupt, SA_INTERRUPT, "rtc", (void *)&rtc_port)) {
764
765
766
767
768 printk(KERN_ERR "rtc: cannot register IRQ %d\n", rtc_irq);
769 return -EIO;
770 }
771no_irq:
772#else
773 if (RTC_IOMAPPED)
774 r = request_region(RTC_PORT(0), RTC_IO_EXTENT, "rtc");
775 else
776 r = request_mem_region(RTC_PORT(0), RTC_IO_EXTENT, "rtc");
777 if (!r) {
778 printk(KERN_ERR "rtc: I/O resource %lx is not free.\n",
779 (long)(RTC_PORT(0)));
780 return -EIO;
781 }
782
783#if RTC_IRQ
784 if(request_irq(RTC_IRQ, rtc_interrupt, SA_INTERRUPT, "rtc", NULL)) {
785
786 printk(KERN_ERR "rtc: IRQ %d is not free.\n", RTC_IRQ);
787 if (RTC_IOMAPPED)
788 release_region(RTC_PORT(0), RTC_IO_EXTENT);
789 else
790 release_mem_region(RTC_PORT(0), RTC_IO_EXTENT);
791 return -EIO;
792 }
793#endif
794
795#endif
796
797 misc_register(&rtc_dev);
798 create_proc_read_entry ("driver/rtc", 0, 0, rtc_read_proc, NULL);
799
800#if defined(__alpha__) || defined(__mips__)
801 rtc_freq = HZ;
802
803
804
805
806 uip_watchdog = jiffies;
807 if (rtc_is_updating() != 0)
808 while (jiffies - uip_watchdog < 2*HZ/100) {
809 barrier();
810 cpu_relax();
811 }
812
813 spin_lock_irq(&rtc_lock);
814 year = CMOS_READ(RTC_YEAR);
815 ctrl = CMOS_READ(RTC_CONTROL);
816 spin_unlock_irq(&rtc_lock);
817
818 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
819 BCD_TO_BIN(year);
820
821 if (year < 20) {
822 epoch = 2000;
823 guess = "SRM (post-2000)";
824 } else if (year >= 20 && year < 48) {
825 epoch = 1980;
826 guess = "ARC console";
827 } else if (year >= 48 && year < 72) {
828 epoch = 1952;
829 guess = "Digital UNIX";
830#if defined(__mips__)
831 } else if (year >= 72 && year < 74) {
832 epoch = 2000;
833 guess = "Digital DECstation";
834#else
835 } else if (year >= 70) {
836 epoch = 1900;
837 guess = "Standard PC (1900)";
838#endif
839 }
840 if (guess)
841 printk(KERN_INFO "rtc: %s epoch (%lu) detected\n", guess, epoch);
842#endif
843#if RTC_IRQ
844 if (rtc_has_irq == 0)
845 goto no_irq2;
846
847 init_timer(&rtc_irq_timer);
848 rtc_irq_timer.function = rtc_dropped_irq;
849 spin_lock_irq(&rtc_lock);
850
851 CMOS_WRITE(((CMOS_READ(RTC_FREQ_SELECT) & 0xF0) | 0x06), RTC_FREQ_SELECT);
852 spin_unlock_irq(&rtc_lock);
853 rtc_freq = 1024;
854no_irq2:
855#endif
856
857 (void) init_sysctl();
858
859 printk(KERN_INFO "Real Time Clock Driver v" RTC_VERSION "\n");
860
861 return 0;
862}
863
864static void __exit rtc_exit (void)
865{
866 cleanup_sysctl();
867 remove_proc_entry ("driver/rtc", NULL);
868 misc_deregister(&rtc_dev);
869
870#ifdef __sparc__
871 if (rtc_has_irq)
872 free_irq (rtc_irq, &rtc_port);
873#else
874 if (RTC_IOMAPPED)
875 release_region(RTC_PORT(0), RTC_IO_EXTENT);
876 else
877 release_mem_region(RTC_PORT(0), RTC_IO_EXTENT);
878#if RTC_IRQ
879 if (rtc_has_irq)
880 free_irq (RTC_IRQ, NULL);
881#endif
882#endif
883}
884
885module_init(rtc_init);
886module_exit(rtc_exit);
887EXPORT_NO_SYMBOLS;
888
889#if RTC_IRQ
890
891
892
893
894
895
896
897
898
899
900
901
902static void rtc_dropped_irq(unsigned long data)
903{
904 unsigned long freq;
905
906 spin_lock_irq (&rtc_lock);
907
908
909 if (rtc_status & RTC_TIMER_ON)
910 mod_timer(&rtc_irq_timer, jiffies + HZ/rtc_freq + 2*HZ/100);
911
912 rtc_irq_data += ((rtc_freq/HZ)<<8);
913 rtc_irq_data &= ~0xff;
914 rtc_irq_data |= (CMOS_READ(RTC_INTR_FLAGS) & 0xF0);
915
916 freq = rtc_freq;
917
918 spin_unlock_irq(&rtc_lock);
919
920 printk(KERN_WARNING "rtc: lost some interrupts at %ldHz.\n", freq);
921
922
923 wake_up_interruptible(&rtc_wait);
924
925 kill_fasync (&rtc_async_queue, SIGIO, POLL_IN);
926}
927#endif
928
929
930
931
932
933static int rtc_proc_output (char *buf)
934{
935#define YN(bit) ((ctrl & bit) ? "yes" : "no")
936#define NY(bit) ((ctrl & bit) ? "no" : "yes")
937 char *p;
938 struct rtc_time tm;
939 unsigned char batt, ctrl;
940 unsigned long freq;
941
942 spin_lock_irq(&rtc_lock);
943 batt = CMOS_READ(RTC_VALID) & RTC_VRT;
944 ctrl = CMOS_READ(RTC_CONTROL);
945 freq = rtc_freq;
946 spin_unlock_irq(&rtc_lock);
947
948 p = buf;
949
950 get_rtc_time(&tm);
951
952
953
954
955
956 p += sprintf(p,
957 "rtc_time\t: %02d:%02d:%02d\n"
958 "rtc_date\t: %04d-%02d-%02d\n"
959 "rtc_epoch\t: %04lu\n",
960 tm.tm_hour, tm.tm_min, tm.tm_sec,
961 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, epoch);
962
963 get_rtc_alm_time(&tm);
964
965
966
967
968
969
970 p += sprintf(p, "alarm\t\t: ");
971 if (tm.tm_hour <= 24)
972 p += sprintf(p, "%02d:", tm.tm_hour);
973 else
974 p += sprintf(p, "**:");
975
976 if (tm.tm_min <= 59)
977 p += sprintf(p, "%02d:", tm.tm_min);
978 else
979 p += sprintf(p, "**:");
980
981 if (tm.tm_sec <= 59)
982 p += sprintf(p, "%02d\n", tm.tm_sec);
983 else
984 p += sprintf(p, "**\n");
985
986 p += sprintf(p,
987 "DST_enable\t: %s\n"
988 "BCD\t\t: %s\n"
989 "24hr\t\t: %s\n"
990 "square_wave\t: %s\n"
991 "alarm_IRQ\t: %s\n"
992 "update_IRQ\t: %s\n"
993 "periodic_IRQ\t: %s\n"
994 "periodic_freq\t: %ld\n"
995 "batt_status\t: %s\n",
996 YN(RTC_DST_EN),
997 NY(RTC_DM_BINARY),
998 YN(RTC_24H),
999 YN(RTC_SQWE),
1000 YN(RTC_AIE),
1001 YN(RTC_UIE),
1002 YN(RTC_PIE),
1003 freq,
1004 batt ? "okay" : "dead");
1005
1006 return p - buf;
1007#undef YN
1008#undef NY
1009}
1010
1011static int rtc_read_proc(char *page, char **start, off_t off,
1012 int count, int *eof, void *data)
1013{
1014 int len = rtc_proc_output (page);
1015 if (len <= off+count) *eof = 1;
1016 *start = page + off;
1017 len -= off;
1018 if (len>count) len = count;
1019 if (len<0) len = 0;
1020 return len;
1021}
1022
1023
1024
1025
1026
1027static inline unsigned char rtc_is_updating(void)
1028{
1029 unsigned char uip;
1030
1031 spin_lock_irq(&rtc_lock);
1032 uip = (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP);
1033 spin_unlock_irq(&rtc_lock);
1034 return uip;
1035}
1036
1037static void get_rtc_time(struct rtc_time *rtc_tm)
1038{
1039 unsigned long uip_watchdog = jiffies;
1040 unsigned char ctrl;
1041#ifdef CONFIG_DECSTATION
1042 unsigned int real_year;
1043#endif
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055 if (rtc_is_updating() != 0)
1056 while (jiffies - uip_watchdog < 2*HZ/100) {
1057 barrier();
1058 cpu_relax();
1059 }
1060
1061
1062
1063
1064
1065
1066
1067 spin_lock_irq(&rtc_lock);
1068 rtc_tm->tm_sec = CMOS_READ(RTC_SECONDS);
1069 rtc_tm->tm_min = CMOS_READ(RTC_MINUTES);
1070 rtc_tm->tm_hour = CMOS_READ(RTC_HOURS);
1071 rtc_tm->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH);
1072 rtc_tm->tm_mon = CMOS_READ(RTC_MONTH);
1073 rtc_tm->tm_year = CMOS_READ(RTC_YEAR);
1074#ifdef CONFIG_DECSTATION
1075 real_year = CMOS_READ(RTC_DEC_YEAR);
1076#endif
1077 ctrl = CMOS_READ(RTC_CONTROL);
1078 spin_unlock_irq(&rtc_lock);
1079
1080 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
1081 {
1082 BCD_TO_BIN(rtc_tm->tm_sec);
1083 BCD_TO_BIN(rtc_tm->tm_min);
1084 BCD_TO_BIN(rtc_tm->tm_hour);
1085 BCD_TO_BIN(rtc_tm->tm_mday);
1086 BCD_TO_BIN(rtc_tm->tm_mon);
1087 BCD_TO_BIN(rtc_tm->tm_year);
1088 }
1089
1090#ifdef CONFIG_DECSTATION
1091 rtc_tm->tm_year += real_year - 72;
1092#endif
1093
1094
1095
1096
1097
1098 if ((rtc_tm->tm_year += (epoch - 1900)) <= 69)
1099 rtc_tm->tm_year += 100;
1100
1101 rtc_tm->tm_mon--;
1102}
1103
1104static void get_rtc_alm_time(struct rtc_time *alm_tm)
1105{
1106 unsigned char ctrl;
1107
1108
1109
1110
1111
1112 spin_lock_irq(&rtc_lock);
1113 alm_tm->tm_sec = CMOS_READ(RTC_SECONDS_ALARM);
1114 alm_tm->tm_min = CMOS_READ(RTC_MINUTES_ALARM);
1115 alm_tm->tm_hour = CMOS_READ(RTC_HOURS_ALARM);
1116 ctrl = CMOS_READ(RTC_CONTROL);
1117 spin_unlock_irq(&rtc_lock);
1118
1119 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
1120 {
1121 BCD_TO_BIN(alm_tm->tm_sec);
1122 BCD_TO_BIN(alm_tm->tm_min);
1123 BCD_TO_BIN(alm_tm->tm_hour);
1124 }
1125}
1126
1127#if RTC_IRQ
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138static void mask_rtc_irq_bit(unsigned char bit)
1139{
1140 unsigned char val;
1141
1142 spin_lock_irq(&rtc_lock);
1143 val = CMOS_READ(RTC_CONTROL);
1144 val &= ~bit;
1145 CMOS_WRITE(val, RTC_CONTROL);
1146 CMOS_READ(RTC_INTR_FLAGS);
1147
1148 rtc_irq_data = 0;
1149 spin_unlock_irq(&rtc_lock);
1150}
1151
1152static void set_rtc_irq_bit(unsigned char bit)
1153{
1154 unsigned char val;
1155
1156 spin_lock_irq(&rtc_lock);
1157 val = CMOS_READ(RTC_CONTROL);
1158 val |= bit;
1159 CMOS_WRITE(val, RTC_CONTROL);
1160 CMOS_READ(RTC_INTR_FLAGS);
1161
1162 rtc_irq_data = 0;
1163 spin_unlock_irq(&rtc_lock);
1164}
1165#endif
1166
1167MODULE_AUTHOR("Paul Gortmaker");
1168MODULE_LICENSE("GPL");
1169