26d15d18237a0717e80e0db147a3bc52c7ad0aac
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 /proc/rtc 9 * 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. 38 #define RTC_VERSION"1.09" 40 #define RTC_IRQ 8/* Can't see this changing soon. */ 41 #define RTC_IO_EXTENT 0x10/* Only really two ports, but... */ 44 * Note that *all* calls to CMOS_READ and CMOS_WRITE are done with 45 * interrupts disabled. Due to the index-port/data-port (0x70/0x71) 46 * design of the RTC, we don't want two different things trying to 47 * get to it at once. (e.g. the periodic 11 min sync from time.c vs. 51 #include <linux/config.h> 52 #include <linux/types.h> 53 #include <linux/errno.h> 54 #include <linux/miscdevice.h> 55 #include <linux/malloc.h> 56 #include <linux/ioport.h> 57 #include <linux/fcntl.h> 58 #include <linux/mc146818rtc.h> 59 #include <linux/init.h> 60 #include <linux/poll.h> 63 #include <asm/uaccess.h> 64 #include <asm/system.h> 66 /* Adjust starting epoch if ARC console time is being used */ 74 * We sponge a minor off of the misc major. No need slurping 75 * up another valuable major dev number for this. If you add 76 * an ioctl, make sure you don't conflict with SPARC's RTC 80 static struct wait_queue
*rtc_wait
; 82 static struct timer_list rtc_irq_timer
; 84 static long longrtc_llseek(struct file
*file
, loff_t offset
,int origin
); 86 static ssize_t
rtc_read(struct file
*file
,char*buf
, 87 size_t count
, loff_t
*ppos
); 89 static intrtc_ioctl(struct inode
*inode
,struct file
*file
, 90 unsigned int cmd
,unsigned long arg
); 92 static unsigned intrtc_poll(struct file
*file
, poll_table
*wait
); 94 voidget_rtc_time(struct rtc_time
*rtc_tm
); 95 voidget_rtc_alm_time(struct rtc_time
*alm_tm
); 96 voidrtc_dropped_irq(unsigned long data
); 98 voidset_rtc_irq_bit(unsigned char bit
); 99 voidmask_rtc_irq_bit(unsigned char bit
); 101 staticinlineunsigned charrtc_is_updating(void); 104 * Bits in rtc_status. (7 bits of room for future expansion) 107 #define RTC_IS_OPEN 0x01/* means /dev/rtc is in use */ 108 #define RTC_TIMER_ON 0x02/* missed irq timer active */ 110 unsigned char rtc_status
=0;/* bitmapped status byte. */ 111 unsigned long rtc_freq
=0;/* Current periodic IRQ rate */ 112 unsigned long rtc_irq_data
=0;/* our output to the world */ 115 * If this driver ever becomes modularised, it will be really nice 116 * to make the epoch retain its value across module reload... 119 static unsigned long epoch
=1900;/* year corresponding to 0x00 */ 121 unsigned char days_in_mo
[] = 122 {0,31,28,31,30,31,30,31,31,30,31,30,31}; 125 * A very tiny interrupt handler. It runs with SA_INTERRUPT set, 126 * so that there is no possibility of conflicting with the 127 * set_rtc_mmss() call that happens during some timer interrupts. 128 * (See ./arch/XXXX/kernel/time.c for the set_rtc_mmss() function.) 130 * On Alpha we won't get any interrupts anyway, as they all end up 131 * in the system timer code. 135 static voidrtc_interrupt(int irq
,void*dev_id
,struct pt_regs
*regs
) 138 * Can be an alarm interrupt, update complete interrupt, 139 * or a periodic interrupt. We store the status in the 140 * low byte and the number of interrupts received since 141 * the last read in the remainder of rtc_irq_data. 144 rtc_irq_data
+=0x100; 145 rtc_irq_data
&= ~0xff; 146 rtc_irq_data
|= (CMOS_READ(RTC_INTR_FLAGS
) &0xF0); 147 wake_up_interruptible(&rtc_wait
); 149 if(rtc_status
& RTC_TIMER_ON
) { 150 del_timer(&rtc_irq_timer
); 151 rtc_irq_timer
.expires
= jiffies
+ HZ
/rtc_freq
+2*HZ
/100; 152 add_timer(&rtc_irq_timer
); 158 * Now all the various file operations that we export. 159 * They are all useless on Alpha... *sigh*. 162 static long longrtc_llseek(struct file
*file
, loff_t offset
,int origin
) 167 static ssize_t
rtc_read(struct file
*file
,char*buf
, 168 size_t count
, loff_t
*ppos
) 173 struct wait_queue wait
= { current
, NULL
}; 177 if(count
<sizeof(unsigned long)) 180 add_wait_queue(&rtc_wait
, &wait
); 182 current
->state
= TASK_INTERRUPTIBLE
; 184 while((data
=xchg(&rtc_irq_data
,0)) ==0) { 185 if(file
->f_flags
& O_NONBLOCK
) { 189 if(signal_pending(current
)) { 190 retval
= -ERESTARTSYS
; 196 retval
=put_user(data
, (unsigned long*)buf
); 198 retval
=sizeof(unsigned long); 200 current
->state
= TASK_RUNNING
; 201 remove_wait_queue(&rtc_wait
, &wait
); 207 static intrtc_ioctl(struct inode
*inode
,struct file
*file
,unsigned int cmd
, 212 struct rtc_time wtime
; 216 case RTC_AIE_OFF
:/* Mask alarm int. enab. bit */ 218 mask_rtc_irq_bit(RTC_AIE
); 221 case RTC_AIE_ON
:/* Allow alarm interrupts. */ 223 set_rtc_irq_bit(RTC_AIE
); 226 case RTC_PIE_OFF
:/* Mask periodic int. enab. bit */ 228 mask_rtc_irq_bit(RTC_PIE
); 229 if(rtc_status
& RTC_TIMER_ON
) { 230 del_timer(&rtc_irq_timer
); 231 rtc_status
&= ~RTC_TIMER_ON
; 235 case RTC_PIE_ON
:/* Allow periodic ints */ 239 * We don't really want Joe User enabling more 240 * than 64Hz of interrupts on a multi-user machine. 242 if((rtc_freq
>64) && (!suser())) 245 if(!(rtc_status
& RTC_TIMER_ON
)) { 246 rtc_status
|= RTC_TIMER_ON
; 247 rtc_irq_timer
.expires
= jiffies
+ HZ
/rtc_freq
+2*HZ
/100; 248 add_timer(&rtc_irq_timer
); 250 set_rtc_irq_bit(RTC_PIE
); 253 case RTC_UIE_OFF
:/* Mask ints from RTC updates. */ 255 mask_rtc_irq_bit(RTC_UIE
); 258 case RTC_UIE_ON
:/* Allow ints for RTC updates. */ 260 set_rtc_irq_bit(RTC_UIE
); 264 case RTC_ALM_READ
:/* Read the present alarm time */ 267 * This returns a struct rtc_time. Reading >= 0xc0 268 * means "don't care" or "match all". Only the tm_hour, 269 * tm_min, and tm_sec values are filled in. 272 get_rtc_alm_time(&wtime
); 275 case RTC_ALM_SET
:/* Store a time into the alarm */ 278 * This expects a struct rtc_time. Writing 0xff means 279 * "don't care" or "match all". Only the tm_hour, 280 * tm_min and tm_sec are used. 282 unsigned char hrs
, min
, sec
; 283 struct rtc_time alm_tm
; 285 if(copy_from_user(&alm_tm
, (struct rtc_time
*)arg
, 286 sizeof(struct rtc_time
))) 289 hrs
= alm_tm
.tm_hour
; 304 if(!(CMOS_READ(RTC_CONTROL
) & RTC_DM_BINARY
) || 311 CMOS_WRITE(hrs
, RTC_HOURS_ALARM
); 312 CMOS_WRITE(min
, RTC_MINUTES_ALARM
); 313 CMOS_WRITE(sec
, RTC_SECONDS_ALARM
); 314 restore_flags(flags
); 318 case RTC_RD_TIME
:/* Read the time/date from RTC */ 320 get_rtc_time(&wtime
); 323 case RTC_SET_TIME
:/* Set the RTC */ 325 struct rtc_time rtc_tm
; 326 unsigned char mon
, day
, hrs
, min
, sec
, leap_yr
; 327 unsigned char save_control
, save_freq_select
; 334 if(copy_from_user(&rtc_tm
, (struct rtc_time
*)arg
, 335 sizeof(struct rtc_time
))) 338 yrs
= rtc_tm
.tm_year
+1900+ ARCFUDGE
; 339 mon
= rtc_tm
.tm_mon
+1;/* tm_mon starts at zero */ 340 day
= rtc_tm
.tm_mday
; 341 hrs
= rtc_tm
.tm_hour
; 348 leap_yr
= ((!(yrs
%4) && (yrs
%100)) || !(yrs
%400)); 350 if((mon
>12) || (day
==0)) 353 if(day
> (days_in_mo
[mon
] + ((mon
==2) && leap_yr
))) 356 if((hrs
>=24) || (min
>=60) || (sec
>=60)) 359 if((yrs
-= epoch
) >255)/* They are unsigned */ 364 if(!(CMOS_READ(RTC_CONTROL
) & RTC_DM_BINARY
) 367 restore_flags(flags
); 381 save_control
=CMOS_READ(RTC_CONTROL
); 382 CMOS_WRITE((save_control
|RTC_SET
), RTC_CONTROL
); 383 save_freq_select
=CMOS_READ(RTC_FREQ_SELECT
); 384 CMOS_WRITE((save_freq_select
|RTC_DIV_RESET2
), RTC_FREQ_SELECT
); 386 CMOS_WRITE(yrs
, RTC_YEAR
); 387 CMOS_WRITE(mon
, RTC_MONTH
); 388 CMOS_WRITE(day
, RTC_DAY_OF_MONTH
); 389 CMOS_WRITE(hrs
, RTC_HOURS
); 390 CMOS_WRITE(min
, RTC_MINUTES
); 391 CMOS_WRITE(sec
, RTC_SECONDS
); 393 CMOS_WRITE(save_control
, RTC_CONTROL
); 394 CMOS_WRITE(save_freq_select
, RTC_FREQ_SELECT
); 396 restore_flags(flags
); 399 case RTC_IRQP_READ
:/* Read the periodic IRQ rate. */ 401 returnput_user(rtc_freq
, (unsigned long*)arg
); 404 case RTC_IRQP_SET
:/* Set periodic IRQ rate. */ 410 * The max we can do is 8192Hz. 412 if((arg
<2) || (arg
>8192)) 415 * We don't really want Joe User generating more 416 * than 64Hz of interrupts on a multi-user machine. 418 if((arg
>64) && (!suser())) 421 while(arg
> (1<<tmp
)) 425 * Check that the input was really a power of 2. 434 val
=CMOS_READ(RTC_FREQ_SELECT
) &0xf0; 436 CMOS_WRITE(val
, RTC_FREQ_SELECT
); 437 restore_flags(flags
); 441 case RTC_EPOCH_READ
:/* Read the epoch. */ 443 returnput_user(epoch
, (unsigned long*)arg
); 445 case RTC_EPOCH_SET
:/* Set the epoch. */ 448 * There were no RTC clocks before 1900. 463 returncopy_to_user((void*)arg
, &wtime
,sizeof wtime
) ? -EFAULT
:0; 467 * We enforce only one user at a time here with the open/close. 468 * Also clear the previous interrupt data on an open, and clean 469 * up things on a close. 470 * On Alpha we just open, for we don't mess with interrups anyway. 473 static intrtc_open(struct inode
*inode
,struct file
*file
) 476 if(rtc_status
& RTC_IS_OPEN
) 479 rtc_status
|= RTC_IS_OPEN
; 485 static intrtc_release(struct inode
*inode
,struct file
*file
) 488 * Turn off all interrupts once the device is no longer 489 * in use, and clear the data. 498 tmp
=CMOS_READ(RTC_CONTROL
); 502 CMOS_WRITE(tmp
, RTC_CONTROL
); 503 CMOS_READ(RTC_INTR_FLAGS
); 504 restore_flags(flags
); 506 if(rtc_status
& RTC_TIMER_ON
) { 507 rtc_status
&= ~RTC_TIMER_ON
; 508 del_timer(&rtc_irq_timer
); 512 rtc_status
&= ~RTC_IS_OPEN
; 518 static unsigned intrtc_poll(struct file
*file
, poll_table
*wait
) 520 poll_wait(file
, &rtc_wait
, wait
); 522 return POLLIN
| POLLRDNORM
; 528 * The various file operations we support. 531 static struct file_operations rtc_fops
= { 535 NULL
,/* No readdir */ 537 NULL
,/* No select on Alpha */ 547 static struct miscdevice rtc_dev
= 554 __initfunc(intrtc_init(void)) 558 unsigned int year
, ctrl
; 559 unsigned long uip_watchdog
; 562 printk(KERN_INFO
"Real Time Clock Driver v%s\n", RTC_VERSION
); 564 if(request_irq(RTC_IRQ
, rtc_interrupt
, SA_INTERRUPT
,"rtc", NULL
)) 566 /* Yeah right, seeing as irq 8 doesn't even hit the bus. */ 567 printk(KERN_ERR
"rtc: IRQ %d is not free.\n", RTC_IRQ
); 571 misc_register(&rtc_dev
); 572 /* Check region? Naaah! Just snarf it up. */ 573 request_region(RTC_PORT(0), RTC_IO_EXTENT
,"rtc"); 577 /* Each operating system on an Alpha uses its own epoch. 578 Let's try to guess which one we are using now. */ 580 uip_watchdog
= jiffies
; 581 if(rtc_is_updating() !=0) 582 while(jiffies
- uip_watchdog
<2*HZ
/100) 587 year
=CMOS_READ(RTC_YEAR
); 588 ctrl
=CMOS_READ(RTC_CONTROL
); 589 restore_flags(flags
); 591 if(!(ctrl
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
) 592 BCD_TO_BIN(year
);/* This should never happen... */ 594 if(year
>10&& year
<44) { 596 guess
="ARC console"; 599 guess
="Digital UNIX"; 602 printk("rtc: %s epoch (%ld) detected\n", guess
, epoch
); 604 init_timer(&rtc_irq_timer
); 605 rtc_irq_timer
.function
= rtc_dropped_irq
; 609 /* Initialize periodic freq. to CMOS reset default, which is 1024Hz */ 610 CMOS_WRITE(((CMOS_READ(RTC_FREQ_SELECT
) &0xF0) |0x06), RTC_FREQ_SELECT
); 611 restore_flags(flags
); 618 * At IRQ rates >= 4096Hz, an interrupt may get lost altogether. 619 * (usually during an IDE disk interrupt, with IRQ unmasking off) 620 * Since the interrupt handler doesn't get called, the IRQ status 621 * byte doesn't get read, and the RTC stops generating interrupts. 622 * A timer is set, and will call this function if/when that happens. 623 * To get it out of this stalled state, we just read the status. 624 * At least a jiffy of interrupts (rtc_freq/HZ) will have been lost. 625 * (You *really* shouldn't be trying to use a non-realtime system 626 * for something that requires a steady > 1KHz signal anyways.) 630 voidrtc_dropped_irq(unsigned long data
) 634 printk(KERN_INFO
"rtc: lost some interrupts at %ldHz.\n", rtc_freq
); 635 del_timer(&rtc_irq_timer
); 636 rtc_irq_timer
.expires
= jiffies
+ HZ
/rtc_freq
+2*HZ
/100; 637 add_timer(&rtc_irq_timer
); 641 rtc_irq_data
+= ((rtc_freq
/HZ
)<<8); 642 rtc_irq_data
&= ~0xff; 643 rtc_irq_data
|= (CMOS_READ(RTC_INTR_FLAGS
) &0xF0);/* restart */ 644 restore_flags(flags
); 649 * Info exported via "/proc/rtc". 652 intget_rtc_status(char*buf
) 656 unsigned char batt
, ctrl
; 661 batt
=CMOS_READ(RTC_VALID
) & RTC_VRT
; 662 ctrl
=CMOS_READ(RTC_CONTROL
); 663 restore_flags(flags
); 670 * There is no way to tell if the luser has the RTC set for local 671 * time or for Universal Standard Time (GMT). Probably local though. 674 "rtc_time\t: %02d:%02d:%02d\n" 675 "rtc_date\t: %04d-%02d-%02d\n", 676 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
, 677 tm
.tm_year
+1900, tm
.tm_mon
+1, tm
.tm_mday
); 679 get_rtc_alm_time(&tm
); 682 * We implicitly assume 24hr mode here. Alarm values >= 0xc0 will 683 * match any value for that particular field. Values that are 684 * greater than a valid time, but less than 0xc0 shouldn't appear. 686 p
+=sprintf(p
,"alarm\t\t: "); 688 p
+=sprintf(p
,"%02d:", tm
.tm_hour
); 690 p
+=sprintf(p
,"**:"); 693 p
+=sprintf(p
,"%02d:", tm
.tm_min
); 695 p
+=sprintf(p
,"**:"); 698 p
+=sprintf(p
,"%02d\n", tm
.tm_sec
); 700 p
+=sprintf(p
,"**\n"); 706 "square_wave\t: %s\n" 709 "periodic_IRQ\t: %s\n" 710 "periodic_freq\t: %ld\n" 711 "batt_status\t: %s\n", 712 (ctrl
& RTC_DST_EN
) ?"yes":"no", 713 (ctrl
& RTC_DM_BINARY
) ?"no":"yes", 714 (ctrl
& RTC_24H
) ?"yes":"no", 715 (ctrl
& RTC_SQWE
) ?"yes":"no", 716 (ctrl
& RTC_AIE
) ?"yes":"no", 717 (ctrl
& RTC_UIE
) ?"yes":"no", 718 (ctrl
& RTC_PIE
) ?"yes":"no", 720 batt
?"okay":"dead"); 726 * Returns true if a clock update is in progress 728 staticinlineunsigned charrtc_is_updating(void) 735 uip
= (CMOS_READ(RTC_FREQ_SELECT
) & RTC_UIP
); 736 restore_flags(flags
); 740 voidget_rtc_time(struct rtc_time
*rtc_tm
) 743 unsigned long flags
, uip_watchdog
= jiffies
; 747 * read RTC once any update in progress is done. The update 748 * can take just over 2ms. We wait 10 to 20ms. There is no need to 749 * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP. 750 * If you need to know *exactly* when a second has started, enable 751 * periodic update complete interrupts, (via ioctl) and then 752 * immediately read /dev/rtc which will block until you get the IRQ. 753 * Once the read clears, read the RTC time (again via ioctl). Easy. 756 if(rtc_is_updating() !=0) 757 while(jiffies
- uip_watchdog
<2*HZ
/100) 761 * Only the values that we read from the RTC are set. We leave 762 * tm_wday, tm_yday and tm_isdst untouched. Even though the 763 * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated 764 * by the RTC when initially set to a non-zero value. 768 rtc_tm
->tm_sec
=CMOS_READ(RTC_SECONDS
); 769 rtc_tm
->tm_min
=CMOS_READ(RTC_MINUTES
); 770 rtc_tm
->tm_hour
=CMOS_READ(RTC_HOURS
); 771 rtc_tm
->tm_mday
=CMOS_READ(RTC_DAY_OF_MONTH
); 772 rtc_tm
->tm_mon
=CMOS_READ(RTC_MONTH
); 773 rtc_tm
->tm_year
=CMOS_READ(RTC_YEAR
); 774 ctrl
=CMOS_READ(RTC_CONTROL
); 775 restore_flags(flags
); 777 if(!(ctrl
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
) 779 BCD_TO_BIN(rtc_tm
->tm_sec
); 780 BCD_TO_BIN(rtc_tm
->tm_min
); 781 BCD_TO_BIN(rtc_tm
->tm_hour
); 782 BCD_TO_BIN(rtc_tm
->tm_mday
); 783 BCD_TO_BIN(rtc_tm
->tm_mon
); 784 BCD_TO_BIN(rtc_tm
->tm_year
); 788 * Account for differences between how the RTC uses the values 789 * and how they are defined in a struct rtc_time; 791 if((rtc_tm
->tm_year
+= epoch
-1900) <=69) 792 rtc_tm
->tm_year
+=100; 794 /* if ARCFUDGE == 0, the optimizer should do away with this */ 795 rtc_tm
->tm_year
-= ARCFUDGE
; 800 voidget_rtc_alm_time(struct rtc_time
*alm_tm
) 806 * Only the values that we read from the RTC are set. That 807 * means only tm_hour, tm_min, and tm_sec. 811 alm_tm
->tm_sec
=CMOS_READ(RTC_SECONDS_ALARM
); 812 alm_tm
->tm_min
=CMOS_READ(RTC_MINUTES_ALARM
); 813 alm_tm
->tm_hour
=CMOS_READ(RTC_HOURS_ALARM
); 814 ctrl
=CMOS_READ(RTC_CONTROL
); 815 restore_flags(flags
); 817 if(!(ctrl
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
) 819 BCD_TO_BIN(alm_tm
->tm_sec
); 820 BCD_TO_BIN(alm_tm
->tm_min
); 821 BCD_TO_BIN(alm_tm
->tm_hour
); 826 * Used to disable/enable interrupts for any one of UIE, AIE, PIE. 827 * Rumour has it that if you frob the interrupt enable/disable 828 * bits in RTC_CONTROL, you should read RTC_INTR_FLAGS, to 829 * ensure you actually start getting interrupts. Probably for 830 * compatibility with older/broken chipset RTC implementations. 831 * We also clear out any old irq data after an ioctl() that 832 * meddles with the interrupt enable/disable bits. 836 voidmask_rtc_irq_bit(unsigned char bit
) 843 val
=CMOS_READ(RTC_CONTROL
); 845 CMOS_WRITE(val
, RTC_CONTROL
); 846 CMOS_READ(RTC_INTR_FLAGS
); 847 restore_flags(flags
); 851 voidset_rtc_irq_bit(unsigned char bit
) 858 val
=CMOS_READ(RTC_CONTROL
); 860 CMOS_WRITE(val
, RTC_CONTROL
); 861 CMOS_READ(RTC_INTR_FLAGS
); 863 restore_flags(flags
);