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