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
41 * Note that *all* calls to CMOS_READ and CMOS_WRITE are done with
42 * interrupts disabled.
43 * REVISIT: Elaborate on OMAP1510 TRM 15uS BUSY access rule.
46 #include <linux/config.h>
47 #include <linux/module.h>
48 #include <linux/kernel.h>
49 #include <linux/types.h>
50 #include <linux/miscdevice.h>
51 #include <linux/ioport.h>
52 #include <linux/fcntl.h>
53 #include <linux/init.h>
54 #include <linux/poll.h>
55 #include <linux/proc_fs.h>
56 #include <linux/spinlock.h>
57 #include <linux/platform_device.h>
58 #include <linux/interrupt.h>
59 #include <linux/rtc.h>
60 #include <linux/bcd.h>
63 #include <asm/uaccess.h>
64 #include <asm/system.h>
65 #include <asm/hardware.h>
68 #include <asm/mach/time.h>
72 extern spinlock_t rtc_lock;
75 /* OMAP RTC register access macros: */
77 #define CMOS_READ(addr) omap_readb(addr)
78 #define CMOS_WRITE(val, addr) omap_writeb(val, addr)
80 static struct fasync_struct *rtc_async_queue;
82 static DECLARE_WAIT_QUEUE_HEAD(rtc_wait);
84 static void get_rtc_time (struct rtc_time *rtc_tm);
85 static void get_rtc_alm_time (struct rtc_time *alm_tm);
87 static void set_rtc_irq_bit(unsigned char bit);
88 static void mask_rtc_irq_bit(unsigned char bit);
90 static int rtc_read_proc(char *page, char **start, off_t off,
91 int count, int *eof, void *data);
94 * Bits in rtc_status. (7 bits of room for future expansion)
97 #define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
100 * REVISIT: fix this comment:
101 * rtc_status is never changed by rtc_interrupt, and ioctl/open/close is
102 * protected by the big kernel lock.
104 static unsigned long rtc_status = 0; /* bitmapped status byte. */
105 static unsigned long rtc_irq_data = 0; /* our output to the world */
108 * If this driver ever becomes modularised, it will be really nice
109 * to make the epoch retain its value across module reload...
112 static unsigned long epoch = 1900; /* year corresponding to 0x00 */
114 static const unsigned char days_in_mo[] =
115 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
118 * A very tiny interrupt handler. It runs with SA_INTERRUPT set.
121 static irqreturn_t rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
124 * Either an alarm interrupt or update complete interrupt.
125 * We store the status in the low byte and the number of
126 * interrupts received since the last read in the remainder
130 spin_lock (&rtc_lock);
132 rtc_irq_data += 0x100;
133 rtc_irq_data &= ~0xff;
134 rtc_irq_data |= CMOS_READ(OMAP_RTC_STATUS_REG);
136 if (rtc_irq_data & OMAP_RTC_STATUS_ALARM)
137 CMOS_WRITE(OMAP_RTC_STATUS_ALARM, OMAP_RTC_STATUS_REG);
139 spin_unlock (&rtc_lock);
141 /* Now do the rest of the actions */
142 wake_up_interruptible(&rtc_wait);
144 kill_fasync (&rtc_async_queue, SIGIO, POLL_IN);
149 * Now all the various file operations that we export.
152 static ssize_t rtc_read(struct file *file, char __user *buf,
153 size_t count, loff_t *ppos)
155 DECLARE_WAITQUEUE(wait, current);
159 if (count < sizeof(unsigned long))
162 add_wait_queue(&rtc_wait, &wait);
163 set_current_state(TASK_INTERRUPTIBLE);
166 spin_lock_irq (&rtc_lock);
172 spin_unlock_irq (&rtc_lock);
174 if (file->f_flags & O_NONBLOCK) {
178 if (signal_pending(current)) {
179 retval = -ERESTARTSYS;
185 spin_unlock_irq (&rtc_lock);
186 retval = put_user(data, (unsigned long __user *)buf);
188 retval = sizeof(unsigned long);
190 set_current_state(TASK_RUNNING);
191 remove_wait_queue(&rtc_wait, &wait);
196 /* convert from userspace struct to hardware BCD-encoded version,
197 * or return error code
199 static int utm2bcd(struct rtc_time __user *arg, struct rtc_time *tm)
201 unsigned char leap_yr;
203 if (copy_from_user(tm, arg, sizeof(struct rtc_time)))
209 if (tm->tm_year < 1970)
212 leap_yr = (!(tm->tm_year % 4) && (tm->tm_year % 100))
213 || !(tm->tm_year % 400);
215 if ((tm->tm_mon > 12) || (tm->tm_mday == 0))
218 if (tm->tm_mday > (days_in_mo[tm->tm_mon] + ((tm->tm_mon == 2) && leap_yr)))
221 if ((tm->tm_hour >= 24) || (tm->tm_min >= 60) || (tm->tm_sec >= 60))
224 if ((tm->tm_year -= epoch) > 255) /* They are unsigned */
227 if (tm->tm_year > 169)
230 if (tm->tm_year >= 100)
233 BIN_TO_BCD(tm->tm_sec);
234 BIN_TO_BCD(tm->tm_min);
235 BIN_TO_BCD(tm->tm_hour);
236 BIN_TO_BCD(tm->tm_mday);
237 BIN_TO_BCD(tm->tm_mon);
238 BIN_TO_BCD(tm->tm_year);
243 static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
246 struct rtc_time wtime;
251 case RTC_AIE_OFF: /* Mask alarm int. enab. bit */
252 mask_rtc_irq_bit(OMAP_RTC_INTERRUPTS_IT_ALARM);
254 case RTC_AIE_ON: /* Allow alarm interrupts. */
255 set_rtc_irq_bit(OMAP_RTC_INTERRUPTS_IT_ALARM);
257 case RTC_UIE_OFF: /* Mask ints from RTC updates. */
258 mask_rtc_irq_bit(OMAP_RTC_INTERRUPTS_IT_TIMER);
260 case RTC_UIE_ON: /* Allow ints for RTC updates. */
261 set_rtc_irq_bit(OMAP_RTC_INTERRUPTS_IT_TIMER);
263 case RTC_ALM_READ: /* Read the present alarm time */
265 * This returns a struct rtc_time. Reading >= 0xc0
266 * means "don't care" or "match all". Only the tm_hour,
267 * tm_min, and tm_sec values are filled in.
269 memset(&wtime, 0, sizeof(struct rtc_time));
270 get_rtc_alm_time(&wtime);
272 case RTC_ALM_SET: /* Store a time into the alarm */
273 status = utm2bcd((void __user *)arg, &wtime);
277 spin_lock_irq(&rtc_lock);
278 CMOS_WRITE(wtime.tm_year, OMAP_RTC_ALARM_YEARS_REG);
279 CMOS_WRITE(wtime.tm_mon, OMAP_RTC_ALARM_MONTHS_REG);
280 CMOS_WRITE(wtime.tm_mday, OMAP_RTC_ALARM_DAYS_REG);
281 CMOS_WRITE(wtime.tm_hour, OMAP_RTC_ALARM_HOURS_REG);
282 CMOS_WRITE(wtime.tm_min, OMAP_RTC_ALARM_MINUTES_REG);
283 CMOS_WRITE(wtime.tm_sec, OMAP_RTC_ALARM_SECONDS_REG);
284 spin_unlock_irq(&rtc_lock);
287 case RTC_RD_TIME: /* Read the time/date from RTC */
288 memset(&wtime, 0, sizeof(struct rtc_time));
289 get_rtc_time(&wtime);
291 case RTC_SET_TIME: /* Set the RTC */
292 if (!capable(CAP_SYS_TIME))
295 status = utm2bcd((void __user *)arg, &wtime);
299 spin_lock_irq(&rtc_lock);
300 save_control = CMOS_READ(OMAP_RTC_CTRL_REG);
301 CMOS_WRITE((save_control & ~OMAP_RTC_CTRL_STOP),
303 CMOS_WRITE(wtime.tm_year, OMAP_RTC_YEARS_REG);
304 CMOS_WRITE(wtime.tm_mon, OMAP_RTC_MONTHS_REG);
305 CMOS_WRITE(wtime.tm_mday, OMAP_RTC_DAYS_REG);
306 CMOS_WRITE(wtime.tm_hour, OMAP_RTC_HOURS_REG);
307 CMOS_WRITE(wtime.tm_min, OMAP_RTC_MINUTES_REG);
308 CMOS_WRITE(wtime.tm_sec, OMAP_RTC_SECONDS_REG);
309 CMOS_WRITE((save_control | OMAP_RTC_CTRL_STOP),
311 spin_unlock_irq(&rtc_lock);
314 case RTC_EPOCH_READ: /* Read the epoch. */
315 status = put_user (epoch, (unsigned long __user *)arg);
317 case RTC_EPOCH_SET: /* Set the epoch. */
318 if (!capable(CAP_SYS_TIME))
322 * There were no RTC clocks before 1900.
335 return copy_to_user((void __user *)arg, &wtime, sizeof wtime)
341 * We enforce only one user at a time here with the open/close.
342 * Also clear the previous interrupt data on an open, and clean
343 * up things on a close.
346 /* We use rtc_lock to protect against concurrent opens. So the BKL is not
347 * needed here. Or anywhere else in this driver. */
348 static int rtc_open(struct inode *inode, struct file *file)
350 spin_lock_irq (&rtc_lock);
352 if (rtc_status & RTC_IS_OPEN)
355 rtc_status |= RTC_IS_OPEN;
358 spin_unlock_irq (&rtc_lock);
362 spin_unlock_irq (&rtc_lock);
366 static int rtc_fasync(int fd, struct file *filp, int on)
368 return fasync_helper (fd, filp, on, &rtc_async_queue);
371 static int rtc_release(struct inode *inode, struct file *file)
376 * Turn off all interrupts once the device is no longer
377 * in use, and clear the data.
380 spin_lock_irq(&rtc_lock);
381 tmp = CMOS_READ(OMAP_RTC_INTERRUPTS_REG);
382 tmp &= ~OMAP_RTC_INTERRUPTS_IT_ALARM;
383 tmp &= ~OMAP_RTC_INTERRUPTS_IT_TIMER;
384 CMOS_WRITE(tmp, OMAP_RTC_INTERRUPTS_REG);
385 spin_unlock_irq(&rtc_lock);
387 if (file->f_flags & FASYNC) {
388 rtc_fasync (-1, file, 0);
391 spin_lock_irq (&rtc_lock);
393 spin_unlock_irq (&rtc_lock);
395 /* No need for locking -- nobody else can do anything until this rmw
396 * is committed, and we don't implement timer support in omap-rtc.
398 rtc_status &= ~RTC_IS_OPEN;
402 /* Called without the kernel lock - fine */
403 static unsigned int rtc_poll(struct file *file, poll_table *wait)
407 poll_wait(file, &rtc_wait, wait);
409 spin_lock_irq (&rtc_lock);
411 spin_unlock_irq (&rtc_lock);
414 return POLLIN | POLLRDNORM;
419 * The various file operations we support.
422 static struct file_operations rtc_fops = {
423 .owner = THIS_MODULE,
429 .release = rtc_release,
430 .fasync = rtc_fasync,
433 static struct miscdevice rtc_dev = {
439 static int __init omap_rtc_probe(struct device *dev)
441 struct platform_device *pdev = to_platform_device(dev);
442 struct resource *res, *mem;
444 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
446 mem = request_mem_region(res->start,
447 res->end - res->start + 1,
452 pr_debug("%s: RTC registers at %x are not free.\n",
453 pdev->name, OMAP_RTC_BASE);
456 dev_set_drvdata(dev, mem);
458 if (CMOS_READ(OMAP_RTC_STATUS_REG) & OMAP_RTC_STATUS_POWER_UP) {
459 pr_info("%s: RTC power up reset detected.\n",
461 /* Clear OMAP_RTC_STATUS_POWER_UP */
462 CMOS_WRITE(OMAP_RTC_STATUS_POWER_UP, OMAP_RTC_STATUS_REG);
465 if (CMOS_READ(OMAP_RTC_STATUS_REG) & OMAP_RTC_STATUS_ALARM) {
466 pr_debug("%s: Clearing RTC ALARM interrupt.\n",
468 /* Clear OMAP_RTC_STATUS_ALARM */
469 CMOS_WRITE(OMAP_RTC_STATUS_ALARM, OMAP_RTC_STATUS_REG);
472 if (request_irq(INT_RTC_TIMER, rtc_interrupt, SA_INTERRUPT,
474 pr_debug("%s: RTC timer interrupt IRQ%d is not free.\n",
475 pdev->name, INT_RTC_TIMER);
479 if (request_irq(INT_RTC_ALARM, rtc_interrupt, SA_INTERRUPT,
481 pr_debug("%s: RTC alarm interrupt IRQ%d is not free.\n",
482 pdev->name, INT_RTC_ALARM);
483 free_irq(INT_RTC_TIMER, NULL);
487 /* On boards with split power, RTC_ON_NOFF resets all but the RTC */
488 if (!(CMOS_READ(OMAP_RTC_CTRL_REG) & OMAP_RTC_CTRL_STOP)) {
489 pr_info("%s: Enabling RTC.\n", pdev->name);
490 CMOS_WRITE(OMAP_RTC_CTRL_STOP, OMAP_RTC_CTRL_REG);
492 pr_info("%s: RTC already running.\n", pdev->name);
494 spin_lock_init(&rtc_lock);
495 misc_register(&rtc_dev);
496 create_proc_read_entry("driver/rtc", 0, NULL, rtc_read_proc, NULL);
501 release_resource(mem);
505 static int __exit omap_rtc_remove(struct device *dev)
507 free_irq (INT_RTC_TIMER, NULL);
508 free_irq (INT_RTC_ALARM, NULL);
510 remove_proc_entry ("driver/rtc", NULL);
511 misc_deregister(&rtc_dev);
513 release_resource(dev_get_drvdata(dev));
518 * Info exported via "/proc/driver/rtc".
521 static int rtc_proc_output (char *buf)
523 #define YN(value) ((value) ? "yes" : "no")
532 * There is no way to tell if the luser has the RTC set for local
533 * time or for Universal Standard Time (GMT). Probably local though.
536 "rtc_time\t: %02d:%02d:%02d\n"
537 "rtc_date\t: %04d-%02d-%02d\n"
538 "rtc_epoch\t: %04lu\n",
539 tm.tm_hour, tm.tm_min, tm.tm_sec,
540 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, epoch);
542 get_rtc_alm_time(&tm);
545 * We implicitly assume 24hr mode here. Alarm values >= 0xc0 will
546 * match any value for that particular field. Values that are
547 * greater than a valid time, but less than 0xc0 shouldn't appear.
550 "alarm_time\t: %02d:%02d:%02d\n"
551 "alarm_date\t: %04d-%02d-%02d\n",
552 tm.tm_hour, tm.tm_min, tm.tm_sec,
553 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
560 "update_rate\t: %ud\n",
563 YN(CMOS_READ(OMAP_RTC_INTERRUPTS_REG) &
564 OMAP_RTC_INTERRUPTS_IT_ALARM),
565 YN(CMOS_READ(OMAP_RTC_INTERRUPTS_REG) &
566 OMAP_RTC_INTERRUPTS_IT_TIMER),
567 CMOS_READ(OMAP_RTC_INTERRUPTS_REG) & 3 /* REVISIT */);
573 static int rtc_read_proc(char *page, char **start, off_t off,
574 int count, int *eof, void *data)
576 int len = rtc_proc_output (page);
578 if (len <= off+count)
590 * Returns true if a clock update is in progress
592 static inline unsigned char rtc_is_updating(void)
596 spin_lock_irq(&rtc_lock);
597 uip = (CMOS_READ(OMAP_RTC_STATUS_REG) & OMAP_RTC_STATUS_BUSY);
598 spin_unlock_irq(&rtc_lock);
602 static void bcd2tm(struct rtc_time *tm)
604 BCD_TO_BIN(tm->tm_sec);
605 BCD_TO_BIN(tm->tm_min);
606 BCD_TO_BIN(tm->tm_hour);
607 BCD_TO_BIN(tm->tm_mday);
608 BCD_TO_BIN(tm->tm_mon);
609 BCD_TO_BIN(tm->tm_year);
612 * Account for differences between how the RTC uses the values
613 * and how they are defined in a struct rtc_time;
615 if ((tm->tm_year += (epoch - 1900)) <= 69)
622 static void get_rtc_time(struct rtc_time *rtc_tm)
626 /* REVISIT: Fix this comment!!!
627 * read RTC once any update in progress is done. The update
628 * can take just over 2ms. We wait 10 to 20ms. There is no need to
629 * to poll-wait (up to 1s - eeccch) for the falling edge of OMAP_RTC_STATUS_BUSY.
630 * If you need to know *exactly* when a second has started, enable
631 * periodic update complete interrupts, (via ioctl) and then
632 * immediately read /dev/rtc which will block until you get the IRQ.
633 * Once the read clears, read the RTC time (again via ioctl). Easy.
636 #if 0 /* REVISIT: This need to do as the TRM says. */
637 unsigned long uip_watchdog = jiffies;
638 if (rtc_is_updating() != 0)
639 while (jiffies - uip_watchdog < 2*HZ/100) {
646 * Only the values that we read from the RTC are set. We leave
647 * tm_wday, tm_yday and tm_isdst untouched. Even though the
648 * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
649 * by the RTC when initially set to a non-zero value.
651 spin_lock_irq(&rtc_lock);
652 rtc_tm->tm_sec = CMOS_READ(OMAP_RTC_SECONDS_REG);
653 rtc_tm->tm_min = CMOS_READ(OMAP_RTC_MINUTES_REG);
654 rtc_tm->tm_hour = CMOS_READ(OMAP_RTC_HOURS_REG);
655 rtc_tm->tm_mday = CMOS_READ(OMAP_RTC_DAYS_REG);
656 rtc_tm->tm_mon = CMOS_READ(OMAP_RTC_MONTHS_REG);
657 rtc_tm->tm_year = CMOS_READ(OMAP_RTC_YEARS_REG);
658 ctrl = CMOS_READ(OMAP_RTC_CTRL_REG);
659 spin_unlock_irq(&rtc_lock);
664 static void get_rtc_alm_time(struct rtc_time *alm_tm)
668 spin_lock_irq(&rtc_lock);
669 alm_tm->tm_sec = CMOS_READ(OMAP_RTC_ALARM_SECONDS_REG);
670 alm_tm->tm_min = CMOS_READ(OMAP_RTC_ALARM_MINUTES_REG);
671 alm_tm->tm_hour = CMOS_READ(OMAP_RTC_ALARM_HOURS_REG);
672 alm_tm->tm_mday = CMOS_READ(OMAP_RTC_ALARM_DAYS_REG);
673 alm_tm->tm_mon = CMOS_READ(OMAP_RTC_ALARM_MONTHS_REG);
674 alm_tm->tm_year = CMOS_READ(OMAP_RTC_ALARM_YEARS_REG);
675 ctrl = CMOS_READ(OMAP_RTC_CTRL_REG);
676 spin_unlock_irq(&rtc_lock);
682 * Used to disable/enable UIE and AIE interrupts.
685 static void mask_rtc_irq_bit(unsigned char bit)
689 spin_lock_irq(&rtc_lock);
690 val = CMOS_READ(OMAP_RTC_INTERRUPTS_REG);
692 CMOS_WRITE(val, OMAP_RTC_INTERRUPTS_REG);
694 spin_unlock_irq(&rtc_lock);
697 static void set_rtc_irq_bit(unsigned char bit)
701 spin_lock_irq(&rtc_lock);
702 val = CMOS_READ(OMAP_RTC_INTERRUPTS_REG);
704 CMOS_WRITE(val, OMAP_RTC_INTERRUPTS_REG);
706 spin_unlock_irq(&rtc_lock);
710 static struct timespec rtc_delta;
712 static int rtc_suspend(struct device *dev, pm_message_t state)
714 struct rtc_time rtc_tm;
715 struct timespec time;
718 get_rtc_time(&rtc_tm);
719 rtc_tm_to_time(&rtc_tm, &time.tv_sec);
721 save_time_delta(&rtc_delta, &time);
726 static int rtc_resume(struct device *dev)
728 struct rtc_time rtc_tm;
729 struct timespec time;
732 get_rtc_time(&rtc_tm);
733 rtc_tm_to_time(&rtc_tm, &time.tv_sec);
735 restore_time_delta(&rtc_delta, &time);
740 #define rtc_suspend NULL
741 #define rtc_resume NULL
744 static struct device_driver omap_rtc_driver = {
746 .bus = &platform_bus_type,
747 .probe = omap_rtc_probe,
748 .remove = __exit_p(omap_rtc_remove),
749 .suspend = rtc_suspend,
750 .resume = rtc_resume,
753 static int __init rtc_init(void)
755 return driver_register(&omap_rtc_driver);
758 static void __exit rtc_exit(void)
760 driver_unregister(&omap_rtc_driver);
763 module_init(rtc_init);
764 module_exit(rtc_exit);
766 MODULE_AUTHOR("George G. Davis (and others)");
767 MODULE_LICENSE("GPL");
768 MODULE_ALIAS_MISCDEV(RTC_MINOR);