2 * TI OMAP Real Time Clock interface for Linux
4 * Copyright (C) 2003 MontaVista Software, Inc.
5 * Author: George G. Davis <gdavis@mvista.com> or <source@mvista.com>
7 * Initially based on linux-2.4.20/drivers/char/rtc.c
8 * Copyright (C) 1996 Paul Gortmaker
10 * This driver allows use of the real time clock (built into
11 * nearly all computers) from user space. It exports the /dev/rtc
12 * interface supporting various ioctl() and also the
13 * /proc/driver/rtc pseudo-file for status information.
15 * The ioctls can be used to set the interrupt behaviour from the
16 * RTC via IRQs. Then the /dev/rtc interface can be used to make
17 * use of RTC interrupts, be they time update or alarm based.
19 * The /dev/rtc interface will block on reads until an interrupt
20 * has been received. If a RTC interrupt has already happened,
21 * it will output an unsigned long and then block. The output value
22 * contains the interrupt status in the low byte and the number of
23 * interrupts since the last read in the remaining high bytes. The
24 * /dev/rtc interface can also be used with the select(2) call.
26 * This program is free software; you can redistribute it and/or
27 * modify it under the terms of the GNU General Public License
28 * as published by the Free Software Foundation; either version
29 * 2 of the License, or (at your option) any later version.
31 * Based on other minimal char device drivers, like Alan's
32 * watchdog, Ted's random, etc. etc.
35 * v1.0 <gdavis@mvista.com> Initial version based on rtc.c v1.10e
36 * <ramakrishnan@india.ti.com> Added support for 2.6 kernel,
37 * - changed the return value of the interrupt handler
40 #define RTC_VERSION "1.0"
43 * Note that *all* calls to CMOS_READ and CMOS_WRITE are done with
44 * interrupts disabled.
45 * REVISIT: Elaborate on OMAP1510 TRM 15uS BUSY access rule.
48 #include <linux/config.h>
49 #include <linux/module.h>
50 #include <linux/kernel.h>
51 #include <linux/types.h>
52 #include <linux/miscdevice.h>
53 #include <linux/ioport.h>
54 #include <linux/fcntl.h>
55 #include <linux/init.h>
56 #include <linux/poll.h>
57 #include <linux/proc_fs.h>
58 #include <linux/spinlock.h>
59 #include <linux/rtc.h>
61 #include <linux/interrupt.h>
62 #include <linux/rtc.h>
65 #include <asm/uaccess.h>
66 #include <asm/system.h>
67 #include <asm/hardware.h>
71 extern spinlock_t rtc_lock;
74 /* OMAP RTC register access macros: */
76 #define CMOS_READ(addr) omap_readb(addr)
77 #define CMOS_WRITE(val, addr) omap_writeb(val, addr)
80 /* Local BCD/BIN conversion macros: */
84 #define BCD_TO_BIN(val) ((val)=((val)&15) + ((val)>>4)*10)
89 #define BIN_TO_BCD(val) ((val)=(((val)/10)<<4) + (val)%10)
93 * We sponge a minor off of the misc major. No need slurping
94 * up another valuable major dev number for this. If you add
95 * an ioctl, make sure you don't conflict with SPARC's RTC
99 static struct fasync_struct *rtc_async_queue;
101 static DECLARE_WAIT_QUEUE_HEAD(rtc_wait);
103 static ssize_t rtc_read(struct file *file, char *buf,
104 size_t count, loff_t *ppos);
106 static int rtc_ioctl(struct inode *inode, struct file *file,
107 unsigned int cmd, unsigned long arg);
109 static unsigned int rtc_poll(struct file *file, poll_table *wait);
111 static void get_rtc_time (struct rtc_time *rtc_tm);
112 static void get_rtc_alm_time (struct rtc_time *alm_tm);
114 static void set_rtc_irq_bit(unsigned char bit);
115 static void mask_rtc_irq_bit(unsigned char bit);
117 static inline unsigned char rtc_is_updating(void);
119 static int rtc_read_proc(char *page, char **start, off_t off,
120 int count, int *eof, void *data);
123 * Bits in rtc_status. (7 bits of room for future expansion)
126 #define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
129 * REVISIT: fix this comment:
130 * rtc_status is never changed by rtc_interrupt, and ioctl/open/close is
131 * protected by the big kernel lock.
133 static unsigned long rtc_status = 0; /* bitmapped status byte. */
134 static unsigned long rtc_irq_data = 0; /* our output to the world */
137 * If this driver ever becomes modularised, it will be really nice
138 * to make the epoch retain its value across module reload...
141 static unsigned long epoch = 1900; /* year corresponding to 0x00 */
143 static const unsigned char days_in_mo[] =
144 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
147 * A very tiny interrupt handler. It runs with SA_INTERRUPT set.
150 static irqreturn_t rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
153 * Either an alarm interrupt or update complete interrupt.
154 * We store the status in the low byte and the number of
155 * interrupts received since the last read in the remainder
159 spin_lock (&rtc_lock);
161 rtc_irq_data += 0x100;
162 rtc_irq_data &= ~0xff;
163 rtc_irq_data |= CMOS_READ(OMAP_RTC_STATUS_REG);
165 if (rtc_irq_data & OMAP_RTC_STATUS_ALARM)
166 CMOS_WRITE(OMAP_RTC_STATUS_ALARM, OMAP_RTC_STATUS_REG);
168 spin_unlock (&rtc_lock);
170 /* Now do the rest of the actions */
171 wake_up_interruptible(&rtc_wait);
173 kill_fasync (&rtc_async_queue, SIGIO, POLL_IN);
178 * Now all the various file operations that we export.
181 static ssize_t rtc_read(struct file *file, char *buf,
182 size_t count, loff_t *ppos)
184 DECLARE_WAITQUEUE(wait, current);
188 if (count < sizeof(unsigned long))
191 add_wait_queue(&rtc_wait, &wait);
192 set_current_state(TASK_INTERRUPTIBLE);
195 spin_lock_irq (&rtc_lock);
201 spin_unlock_irq (&rtc_lock);
203 if (file->f_flags & O_NONBLOCK) {
207 if (signal_pending(current)) {
208 retval = -ERESTARTSYS;
214 spin_unlock_irq (&rtc_lock);
215 retval = put_user(data, (unsigned long *)buf);
217 retval = sizeof(unsigned long);
219 set_current_state(TASK_RUNNING);
220 remove_wait_queue(&rtc_wait, &wait);
225 static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
228 struct rtc_time wtime;
231 case RTC_AIE_OFF: /* Mask alarm int. enab. bit */
233 mask_rtc_irq_bit(OMAP_RTC_INTERRUPTS_IT_ALARM);
236 case RTC_AIE_ON: /* Allow alarm interrupts. */
238 set_rtc_irq_bit(OMAP_RTC_INTERRUPTS_IT_ALARM);
241 case RTC_UIE_OFF: /* Mask ints from RTC updates. */
243 mask_rtc_irq_bit(OMAP_RTC_INTERRUPTS_IT_TIMER);
246 case RTC_UIE_ON: /* Allow ints for RTC updates. */
248 set_rtc_irq_bit(OMAP_RTC_INTERRUPTS_IT_TIMER);
251 case RTC_ALM_READ: /* Read the present alarm time */
254 * This returns a struct rtc_time. Reading >= 0xc0
255 * means "don't care" or "match all". Only the tm_hour,
256 * tm_min, and tm_sec values are filled in.
258 memset(&wtime, 0, sizeof(struct rtc_time));
259 get_rtc_alm_time(&wtime);
262 case RTC_ALM_SET: /* Store a time into the alarm */
264 struct rtc_time alm_tm;
265 unsigned char mon, day, hrs, min, sec, leap_yr;
268 if (copy_from_user(&alm_tm, (struct rtc_time*)arg,
269 sizeof(struct rtc_time)))
272 yrs = alm_tm.tm_year + 1900;
273 mon = alm_tm.tm_mon + 1;
274 day = alm_tm.tm_mday;
275 hrs = alm_tm.tm_hour;
282 leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
284 if ((mon > 12) || (day == 0))
287 if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
290 if ((hrs >= 24) || (min >= 60) || (sec >= 60))
293 if ((yrs -= epoch) > 255) /* They are unsigned */
310 spin_lock_irq(&rtc_lock);
311 CMOS_WRITE(yrs, OMAP_RTC_ALARM_YEARS_REG);
312 CMOS_WRITE(mon, OMAP_RTC_ALARM_MONTHS_REG);
313 CMOS_WRITE(day, OMAP_RTC_ALARM_DAYS_REG);
314 CMOS_WRITE(hrs, OMAP_RTC_ALARM_HOURS_REG);
315 CMOS_WRITE(min, OMAP_RTC_ALARM_MINUTES_REG);
316 CMOS_WRITE(sec, OMAP_RTC_ALARM_SECONDS_REG);
317 spin_unlock_irq(&rtc_lock);
321 case RTC_RD_TIME: /* Read the time/date from RTC */
323 memset(&wtime, 0, sizeof(struct rtc_time));
324 get_rtc_time(&wtime);
327 case RTC_SET_TIME: /* Set the RTC */
329 struct rtc_time rtc_tm;
330 unsigned char mon, day, hrs, min, sec, leap_yr;
331 unsigned char save_control;
334 if (!capable(CAP_SYS_TIME))
337 if (copy_from_user(&rtc_tm, (struct rtc_time*)arg,
338 sizeof(struct rtc_time)))
341 yrs = rtc_tm.tm_year + 1900;
342 mon = rtc_tm.tm_mon + 1; /* tm_mon starts at zero */
343 day = rtc_tm.tm_mday;
344 hrs = rtc_tm.tm_hour;
351 leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
353 if ((mon > 12) || (day == 0))
356 if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
359 if ((hrs >= 24) || (min >= 60) || (sec >= 60))
362 if ((yrs -= epoch) > 255) /* They are unsigned */
379 spin_lock_irq(&rtc_lock);
380 save_control = CMOS_READ(OMAP_RTC_CTRL_REG);
381 CMOS_WRITE((save_control & ~OMAP_RTC_CTRL_STOP),
383 CMOS_WRITE(yrs, OMAP_RTC_YEARS_REG);
384 CMOS_WRITE(mon, OMAP_RTC_MONTHS_REG);
385 CMOS_WRITE(day, OMAP_RTC_DAYS_REG);
386 CMOS_WRITE(hrs, OMAP_RTC_HOURS_REG);
387 CMOS_WRITE(min, OMAP_RTC_MINUTES_REG);
388 CMOS_WRITE(sec, OMAP_RTC_SECONDS_REG);
389 CMOS_WRITE((save_control | OMAP_RTC_CTRL_STOP),
391 spin_unlock_irq(&rtc_lock);
395 case RTC_EPOCH_READ: /* Read the epoch. */
397 return put_user (epoch, (unsigned long *)arg);
399 case RTC_EPOCH_SET: /* Set the epoch. */
402 * There were no RTC clocks before 1900.
407 if (!capable(CAP_SYS_TIME))
414 #if !defined(CONFIG_ARCH_OMAP)
420 return copy_to_user((void *)arg, &wtime, sizeof wtime) ? -EFAULT : 0;
424 * We enforce only one user at a time here with the open/close.
425 * Also clear the previous interrupt data on an open, and clean
426 * up things on a close.
429 /* We use rtc_lock to protect against concurrent opens. So the BKL is not
430 * needed here. Or anywhere else in this driver. */
431 static int rtc_open(struct inode *inode, struct file *file)
433 spin_lock_irq (&rtc_lock);
435 if(rtc_status & RTC_IS_OPEN)
438 rtc_status |= RTC_IS_OPEN;
441 spin_unlock_irq (&rtc_lock);
445 spin_unlock_irq (&rtc_lock);
449 static int rtc_fasync (int fd, struct file *filp, int on)
452 return fasync_helper (fd, filp, on, &rtc_async_queue);
455 static int rtc_release(struct inode *inode, struct file *file)
460 * Turn off all interrupts once the device is no longer
461 * in use, and clear the data.
464 spin_lock_irq(&rtc_lock);
465 tmp = CMOS_READ(OMAP_RTC_INTERRUPTS_REG);
466 tmp &= ~OMAP_RTC_INTERRUPTS_IT_ALARM;
467 tmp &= ~OMAP_RTC_INTERRUPTS_IT_TIMER;
468 CMOS_WRITE(tmp, OMAP_RTC_INTERRUPTS_REG);
469 spin_unlock_irq(&rtc_lock);
471 if (file->f_flags & FASYNC) {
472 rtc_fasync (-1, file, 0);
475 spin_lock_irq (&rtc_lock);
477 spin_unlock_irq (&rtc_lock);
479 /* No need for locking -- nobody else can do anything until this rmw
480 * is committed, and we don't implement timer support in omap-rtc.
482 rtc_status &= ~RTC_IS_OPEN;
486 /* Called without the kernel lock - fine */
487 static unsigned int rtc_poll(struct file *file, poll_table *wait)
491 poll_wait(file, &rtc_wait, wait);
493 spin_lock_irq (&rtc_lock);
495 spin_unlock_irq (&rtc_lock);
498 return POLLIN | POLLRDNORM;
503 * The various file operations we support.
506 static struct file_operations rtc_fops = {
513 release: rtc_release,
517 static struct miscdevice rtc_dev=
524 static int __init rtc_init(void)
526 if (!request_region(OMAP_RTC_VIRT_BASE, OMAP_RTC_SIZE,
528 printk(KERN_ERR "%s: RTC I/O port %d is not free.\n",
529 rtc_dev.name, OMAP_RTC_VIRT_BASE);
533 if (CMOS_READ(OMAP_RTC_STATUS_REG) & OMAP_RTC_STATUS_POWER_UP) {
534 printk(KERN_WARNING "%s: RTC power up reset detected.\n",
536 /* Clear OMAP_RTC_STATUS_POWER_UP */
537 CMOS_WRITE(OMAP_RTC_STATUS_POWER_UP, OMAP_RTC_STATUS_REG);
540 if (CMOS_READ(OMAP_RTC_STATUS_REG) & OMAP_RTC_STATUS_ALARM) {
541 printk(KERN_WARNING "%s: Clearing RTC ALARM interrupt.\n",
543 /* Clear OMAP_RTC_STATUS_ALARM */
544 CMOS_WRITE(OMAP_RTC_STATUS_ALARM, OMAP_RTC_STATUS_REG);
547 if (!(CMOS_READ(OMAP_RTC_CTRL_REG) & OMAP_RTC_CTRL_STOP)) {
548 printk(KERN_INFO "%s: Enabling RTC.\n", rtc_dev.name);
549 CMOS_WRITE(OMAP_RTC_CTRL_STOP, OMAP_RTC_CTRL_REG);
552 if (request_irq(INT_RTC_TIMER, rtc_interrupt, SA_INTERRUPT,
553 rtc_dev.name, NULL)) {
554 printk(KERN_ERR "%s: RTC timer interrupt IRQ%d is not free.\n",
555 rtc_dev.name, INT_RTC_TIMER);
556 release_region(OMAP_RTC_VIRT_BASE, OMAP_RTC_SIZE);
560 if (request_irq(INT_RTC_ALARM, rtc_interrupt, SA_INTERRUPT,
561 "omap-rtc alarm", NULL)) {
562 printk(KERN_ERR "%s: RTC alarm interrupt IRQ%d is not free.\n",
563 rtc_dev.name, INT_RTC_ALARM);
564 release_region(OMAP_RTC_VIRT_BASE, OMAP_RTC_SIZE);
568 spin_lock_init(&rtc_lock);
569 misc_register(&rtc_dev);
570 create_proc_read_entry ("driver/rtc", 0, 0, rtc_read_proc, NULL);
572 printk(KERN_INFO "Real Time Clock Driver v" RTC_VERSION "\n");
577 static void __exit rtc_exit (void)
579 free_irq (INT_RTC_TIMER, NULL);
580 free_irq (INT_RTC_ALARM, NULL);
582 remove_proc_entry ("driver/rtc", NULL);
583 misc_deregister(&rtc_dev);
585 release_region (OMAP_RTC_VIRT_BASE, OMAP_RTC_SIZE);
589 * Info exported via "/proc/driver/rtc".
592 static int rtc_proc_output (char *buf)
594 #define YN(value) ((value) ? "yes" : "no")
595 #define NY(value) ((value) ? "no" : "yes")
604 * There is no way to tell if the luser has the RTC set for local
605 * time or for Universal Standard Time (GMT). Probably local though.
608 "rtc_time\t: %02d:%02d:%02d\n"
609 "rtc_date\t: %04d-%02d-%02d\n"
610 "rtc_epoch\t: %04lu\n",
611 tm.tm_hour, tm.tm_min, tm.tm_sec,
612 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, epoch);
614 get_rtc_alm_time(&tm);
617 * We implicitly assume 24hr mode here. Alarm values >= 0xc0 will
618 * match any value for that particular field. Values that are
619 * greater than a valid time, but less than 0xc0 shouldn't appear.
622 "alarm_time\t: %02d:%02d:%02d\n"
623 "alarm_date\t: %04d-%02d-%02d\n",
624 tm.tm_hour, tm.tm_min, tm.tm_sec,
625 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
632 "update_rate\t: %ud\n",
635 YN(CMOS_READ(OMAP_RTC_INTERRUPTS_REG) &
636 OMAP_RTC_INTERRUPTS_IT_ALARM),
637 YN(CMOS_READ(OMAP_RTC_INTERRUPTS_REG) &
638 OMAP_RTC_INTERRUPTS_IT_TIMER),
639 CMOS_READ(OMAP_RTC_INTERRUPTS_REG) & 3 /* REVISIT */);
646 static int rtc_read_proc(char *page, char **start, off_t off,
647 int count, int *eof, void *data)
649 int len = rtc_proc_output (page);
650 if (len <= off+count) *eof = 1;
653 if (len>count) len = count;
659 * Returns true if a clock update is in progress
661 /* FIXME shouldn't this be above rtc_init to make it fully inlined? */
662 static inline unsigned char rtc_is_updating(void)
666 spin_lock_irq(&rtc_lock);
667 uip = (CMOS_READ(OMAP_RTC_STATUS_REG) & OMAP_RTC_STATUS_BUSY);
668 spin_unlock_irq(&rtc_lock);
672 static void get_rtc_time(struct rtc_time *rtc_tm)
676 /* REVISIT: Fix this comment!!!
677 * read RTC once any update in progress is done. The update
678 * can take just over 2ms. We wait 10 to 20ms. There is no need to
679 * to poll-wait (up to 1s - eeccch) for the falling edge of OMAP_RTC_STATUS_BUSY.
680 * If you need to know *exactly* when a second has started, enable
681 * periodic update complete interrupts, (via ioctl) and then
682 * immediately read /dev/rtc which will block until you get the IRQ.
683 * Once the read clears, read the RTC time (again via ioctl). Easy.
686 #if 0 /* REVISIT: This need to do as the TRM says. */
687 unsigned long uip_watchdog = jiffies;
688 if (rtc_is_updating() != 0)
689 while (jiffies - uip_watchdog < 2*HZ/100) {
696 * Only the values that we read from the RTC are set. We leave
697 * tm_wday, tm_yday and tm_isdst untouched. Even though the
698 * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
699 * by the RTC when initially set to a non-zero value.
701 spin_lock_irq(&rtc_lock);
702 rtc_tm->tm_sec = CMOS_READ(OMAP_RTC_SECONDS_REG);
703 rtc_tm->tm_min = CMOS_READ(OMAP_RTC_MINUTES_REG);
704 rtc_tm->tm_hour = CMOS_READ(OMAP_RTC_HOURS_REG);
705 rtc_tm->tm_mday = CMOS_READ(OMAP_RTC_DAYS_REG);
706 rtc_tm->tm_mon = CMOS_READ(OMAP_RTC_MONTHS_REG);
707 rtc_tm->tm_year = CMOS_READ(OMAP_RTC_YEARS_REG);
708 ctrl = CMOS_READ(OMAP_RTC_CTRL_REG);
709 spin_unlock_irq(&rtc_lock);
711 BCD_TO_BIN(rtc_tm->tm_sec);
712 BCD_TO_BIN(rtc_tm->tm_min);
713 BCD_TO_BIN(rtc_tm->tm_hour);
714 BCD_TO_BIN(rtc_tm->tm_mday);
715 BCD_TO_BIN(rtc_tm->tm_mon);
716 BCD_TO_BIN(rtc_tm->tm_year);
719 * Account for differences between how the RTC uses the values
720 * and how they are defined in a struct rtc_time;
722 if ((rtc_tm->tm_year += (epoch - 1900)) <= 69)
723 rtc_tm->tm_year += 100;
728 static void get_rtc_alm_time(struct rtc_time *alm_tm)
732 spin_lock_irq(&rtc_lock);
733 alm_tm->tm_sec = CMOS_READ(OMAP_RTC_ALARM_SECONDS_REG);
734 alm_tm->tm_min = CMOS_READ(OMAP_RTC_ALARM_MINUTES_REG);
735 alm_tm->tm_hour = CMOS_READ(OMAP_RTC_ALARM_HOURS_REG);
736 alm_tm->tm_mday = CMOS_READ(OMAP_RTC_ALARM_DAYS_REG);
737 alm_tm->tm_mon = CMOS_READ(OMAP_RTC_ALARM_MONTHS_REG);
738 alm_tm->tm_year = CMOS_READ(OMAP_RTC_ALARM_YEARS_REG);
739 ctrl = CMOS_READ(OMAP_RTC_CTRL_REG);
740 spin_unlock_irq(&rtc_lock);
742 BCD_TO_BIN(alm_tm->tm_sec);
743 BCD_TO_BIN(alm_tm->tm_min);
744 BCD_TO_BIN(alm_tm->tm_hour);
745 BCD_TO_BIN(alm_tm->tm_mday);
746 BCD_TO_BIN(alm_tm->tm_mon);
747 BCD_TO_BIN(alm_tm->tm_year);
750 * Account for differences between how the RTC uses the values
751 * and how they are defined in a struct rtc_time;
753 if ((alm_tm->tm_year += (epoch - 1900)) <= 69)
754 alm_tm->tm_year += 100;
760 * Used to disable/enable UIE and AIE interrupts.
763 static void mask_rtc_irq_bit(unsigned char bit)
767 spin_lock_irq(&rtc_lock);
768 val = CMOS_READ(OMAP_RTC_INTERRUPTS_REG);
770 CMOS_WRITE(val, OMAP_RTC_INTERRUPTS_REG);
772 spin_unlock_irq(&rtc_lock);
775 static void set_rtc_irq_bit(unsigned char bit)
779 spin_lock_irq(&rtc_lock);
780 val = CMOS_READ(OMAP_RTC_INTERRUPTS_REG);
782 CMOS_WRITE(val, OMAP_RTC_INTERRUPTS_REG);
784 spin_unlock_irq(&rtc_lock);
787 MODULE_AUTHOR("George G. Davis");
788 MODULE_LICENSE("GPL");
790 module_init(rtc_init);
791 module_exit(rtc_exit);