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;
74 static int omap_rtc_alarm = NO_IRQ;
75 static int omap_rtc_timer = NO_IRQ;
78 /* OMAP RTC register access macros: */
80 #define CMOS_READ(addr) omap_readb(addr)
81 #define CMOS_WRITE(val, addr) omap_writeb(val, addr)
83 static struct fasync_struct *rtc_async_queue;
85 static DECLARE_WAIT_QUEUE_HEAD(rtc_wait);
87 static void get_rtc_time (struct rtc_time *rtc_tm);
88 static void get_rtc_alm_time (struct rtc_time *alm_tm);
90 static void set_rtc_irq_bit(unsigned char bit);
91 static void mask_rtc_irq_bit(unsigned char bit);
93 static int rtc_read_proc(char *page, char **start, off_t off,
94 int count, int *eof, void *data);
97 * Bits in rtc_status. (7 bits of room for future expansion)
100 #define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
103 * REVISIT: fix this comment:
104 * rtc_status is never changed by rtc_interrupt, and ioctl/open/close is
105 * protected by the big kernel lock.
107 static unsigned long rtc_status = 0; /* bitmapped status byte. */
108 static unsigned long rtc_irq_data = 0; /* our output to the world */
111 * If this driver ever becomes modularised, it will be really nice
112 * to make the epoch retain its value across module reload...
115 static unsigned long epoch = 1900; /* year corresponding to 0x00 */
117 static const unsigned char days_in_mo[] =
118 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
121 * A very tiny interrupt handler. It runs with SA_INTERRUPT set.
124 static irqreturn_t rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
127 * Either an alarm interrupt or update complete interrupt.
128 * We store the status in the low byte and the number of
129 * interrupts received since the last read in the remainder
133 spin_lock (&rtc_lock);
135 rtc_irq_data += 0x100;
136 rtc_irq_data &= ~0xff;
137 rtc_irq_data |= CMOS_READ(OMAP_RTC_STATUS_REG);
139 if (rtc_irq_data & OMAP_RTC_STATUS_ALARM)
140 CMOS_WRITE(OMAP_RTC_STATUS_ALARM, OMAP_RTC_STATUS_REG);
142 spin_unlock (&rtc_lock);
144 /* Now do the rest of the actions */
145 wake_up_interruptible(&rtc_wait);
147 kill_fasync (&rtc_async_queue, SIGIO, POLL_IN);
152 * Now all the various file operations that we export.
155 static ssize_t rtc_read(struct file *file, char __user *buf,
156 size_t count, loff_t *ppos)
158 DECLARE_WAITQUEUE(wait, current);
162 if (count < sizeof(unsigned long))
165 add_wait_queue(&rtc_wait, &wait);
166 set_current_state(TASK_INTERRUPTIBLE);
169 spin_lock_irq (&rtc_lock);
175 spin_unlock_irq (&rtc_lock);
177 if (file->f_flags & O_NONBLOCK) {
181 if (signal_pending(current)) {
182 retval = -ERESTARTSYS;
188 spin_unlock_irq (&rtc_lock);
189 retval = put_user(data, (unsigned long __user *)buf);
191 retval = sizeof(unsigned long);
193 set_current_state(TASK_RUNNING);
194 remove_wait_queue(&rtc_wait, &wait);
199 /* convert from userspace struct to hardware BCD-encoded version,
200 * or return error code
202 static int utm2bcd(struct rtc_time __user *arg, struct rtc_time *tm)
204 unsigned char leap_yr;
206 if (copy_from_user(tm, arg, sizeof(struct rtc_time)))
212 if (tm->tm_year < 1970)
215 leap_yr = (!(tm->tm_year % 4) && (tm->tm_year % 100))
216 || !(tm->tm_year % 400);
218 if ((tm->tm_mon > 12) || (tm->tm_mday == 0))
221 if (tm->tm_mday > (days_in_mo[tm->tm_mon] + ((tm->tm_mon == 2) && leap_yr)))
224 if ((tm->tm_hour >= 24) || (tm->tm_min >= 60) || (tm->tm_sec >= 60))
227 if ((tm->tm_year -= epoch) > 255) /* They are unsigned */
230 if (tm->tm_year > 169)
233 if (tm->tm_year >= 100)
236 BIN_TO_BCD(tm->tm_sec);
237 BIN_TO_BCD(tm->tm_min);
238 BIN_TO_BCD(tm->tm_hour);
239 BIN_TO_BCD(tm->tm_mday);
240 BIN_TO_BCD(tm->tm_mon);
241 BIN_TO_BCD(tm->tm_year);
246 static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
249 struct rtc_time wtime;
254 case RTC_AIE_OFF: /* Mask alarm int. enab. bit */
255 mask_rtc_irq_bit(OMAP_RTC_INTERRUPTS_IT_ALARM);
257 case RTC_AIE_ON: /* Allow alarm interrupts. */
258 set_rtc_irq_bit(OMAP_RTC_INTERRUPTS_IT_ALARM);
260 case RTC_UIE_OFF: /* Mask ints from RTC updates. */
261 mask_rtc_irq_bit(OMAP_RTC_INTERRUPTS_IT_TIMER);
263 case RTC_UIE_ON: /* Allow ints for RTC updates. */
264 set_rtc_irq_bit(OMAP_RTC_INTERRUPTS_IT_TIMER);
266 case RTC_ALM_READ: /* Read the present alarm time */
268 * This returns a struct rtc_time. Reading >= 0xc0
269 * means "don't care" or "match all". Only the tm_hour,
270 * tm_min, and tm_sec values are filled in.
272 memset(&wtime, 0, sizeof(struct rtc_time));
273 get_rtc_alm_time(&wtime);
275 case RTC_ALM_SET: /* Store a time into the alarm */
276 status = utm2bcd((void __user *)arg, &wtime);
280 spin_lock_irq(&rtc_lock);
281 CMOS_WRITE(wtime.tm_year, OMAP_RTC_ALARM_YEARS_REG);
282 CMOS_WRITE(wtime.tm_mon, OMAP_RTC_ALARM_MONTHS_REG);
283 CMOS_WRITE(wtime.tm_mday, OMAP_RTC_ALARM_DAYS_REG);
284 CMOS_WRITE(wtime.tm_hour, OMAP_RTC_ALARM_HOURS_REG);
285 CMOS_WRITE(wtime.tm_min, OMAP_RTC_ALARM_MINUTES_REG);
286 CMOS_WRITE(wtime.tm_sec, OMAP_RTC_ALARM_SECONDS_REG);
287 spin_unlock_irq(&rtc_lock);
290 case RTC_RD_TIME: /* Read the time/date from RTC */
291 memset(&wtime, 0, sizeof(struct rtc_time));
292 get_rtc_time(&wtime);
294 case RTC_SET_TIME: /* Set the RTC */
295 if (!capable(CAP_SYS_TIME))
298 status = utm2bcd((void __user *)arg, &wtime);
302 spin_lock_irq(&rtc_lock);
303 save_control = CMOS_READ(OMAP_RTC_CTRL_REG);
304 CMOS_WRITE((save_control & ~OMAP_RTC_CTRL_STOP),
306 CMOS_WRITE(wtime.tm_year, OMAP_RTC_YEARS_REG);
307 CMOS_WRITE(wtime.tm_mon, OMAP_RTC_MONTHS_REG);
308 CMOS_WRITE(wtime.tm_mday, OMAP_RTC_DAYS_REG);
309 CMOS_WRITE(wtime.tm_hour, OMAP_RTC_HOURS_REG);
310 CMOS_WRITE(wtime.tm_min, OMAP_RTC_MINUTES_REG);
311 CMOS_WRITE(wtime.tm_sec, OMAP_RTC_SECONDS_REG);
312 CMOS_WRITE((save_control | OMAP_RTC_CTRL_STOP),
314 spin_unlock_irq(&rtc_lock);
317 case RTC_EPOCH_READ: /* Read the epoch. */
318 status = put_user (epoch, (unsigned long __user *)arg);
320 case RTC_EPOCH_SET: /* Set the epoch. */
321 if (!capable(CAP_SYS_TIME))
325 * There were no RTC clocks before 1900.
338 return copy_to_user((void __user *)arg, &wtime, sizeof wtime)
344 * We enforce only one user at a time here with the open/close.
345 * Also clear the previous interrupt data on an open, and clean
346 * up things on a close.
349 /* We use rtc_lock to protect against concurrent opens. So the BKL is not
350 * needed here. Or anywhere else in this driver. */
351 static int rtc_open(struct inode *inode, struct file *file)
353 spin_lock_irq (&rtc_lock);
355 if (rtc_status & RTC_IS_OPEN)
358 rtc_status |= RTC_IS_OPEN;
361 spin_unlock_irq (&rtc_lock);
365 spin_unlock_irq (&rtc_lock);
369 static int rtc_fasync(int fd, struct file *filp, int on)
371 return fasync_helper (fd, filp, on, &rtc_async_queue);
374 static int rtc_release(struct inode *inode, struct file *file)
379 * Turn off all interrupts once the device is no longer
380 * in use, and clear the data.
383 spin_lock_irq(&rtc_lock);
384 tmp = CMOS_READ(OMAP_RTC_INTERRUPTS_REG);
385 tmp &= ~OMAP_RTC_INTERRUPTS_IT_ALARM;
386 tmp &= ~OMAP_RTC_INTERRUPTS_IT_TIMER;
387 CMOS_WRITE(tmp, OMAP_RTC_INTERRUPTS_REG);
388 spin_unlock_irq(&rtc_lock);
390 if (file->f_flags & FASYNC) {
391 rtc_fasync (-1, file, 0);
394 spin_lock_irq (&rtc_lock);
396 spin_unlock_irq (&rtc_lock);
398 /* No need for locking -- nobody else can do anything until this rmw
399 * is committed, and we don't implement timer support in omap-rtc.
401 rtc_status &= ~RTC_IS_OPEN;
405 /* Called without the kernel lock - fine */
406 static unsigned int rtc_poll(struct file *file, poll_table *wait)
410 poll_wait(file, &rtc_wait, wait);
412 spin_lock_irq (&rtc_lock);
414 spin_unlock_irq (&rtc_lock);
417 return POLLIN | POLLRDNORM;
422 * The various file operations we support.
425 static struct file_operations rtc_fops = {
426 .owner = THIS_MODULE,
432 .release = rtc_release,
433 .fasync = rtc_fasync,
436 static struct miscdevice rtc_dev = {
442 static int __init omap_rtc_probe(struct device *dev)
444 struct platform_device *pdev = to_platform_device(dev);
445 struct resource *res, *mem;
449 omap_rtc_timer = platform_get_irq(pdev, 0);
450 if (omap_rtc_timer <= 0) {
451 dev_err(&pdev->dev, "no irq for rtc timer\n");
455 omap_rtc_alarm = platform_get_irq(pdev, 1);
456 if (omap_rtc_alarm <= 0) {
457 dev_err(&pdev->dev, "no irq for alarm\n");
461 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
463 mem = request_mem_region(res->start,
464 res->end - res->start + 1,
469 pr_debug("%s: RTC registers at %x are not free.\n",
470 pdev->name, OMAP_RTC_BASE);
473 dev_set_drvdata(dev, mem);
475 if (CMOS_READ(OMAP_RTC_STATUS_REG) & OMAP_RTC_STATUS_POWER_UP) {
476 pr_info("%s: RTC power up reset detected.\n",
478 /* Clear OMAP_RTC_STATUS_POWER_UP */
479 CMOS_WRITE(OMAP_RTC_STATUS_POWER_UP, OMAP_RTC_STATUS_REG);
482 if (CMOS_READ(OMAP_RTC_STATUS_REG) & OMAP_RTC_STATUS_ALARM) {
483 pr_debug("%s: Clearing RTC ALARM interrupt.\n",
485 /* Clear OMAP_RTC_STATUS_ALARM */
486 CMOS_WRITE(OMAP_RTC_STATUS_ALARM, OMAP_RTC_STATUS_REG);
489 if (request_irq(omap_rtc_timer, rtc_interrupt, SA_INTERRUPT,
491 pr_debug("%s: RTC timer interrupt IRQ%d is not free.\n",
492 pdev->name, omap_rtc_timer);
496 if (request_irq(omap_rtc_alarm, rtc_interrupt, SA_INTERRUPT,
498 pr_debug("%s: RTC alarm interrupt IRQ%d is not free.\n",
499 pdev->name, omap_rtc_alarm);
500 free_irq(omap_rtc_timer, NULL);
504 /* On boards with split power, RTC_ON_NOFF resets all but the RTC */
505 if (!(CMOS_READ(OMAP_RTC_CTRL_REG) & OMAP_RTC_CTRL_STOP)) {
506 pr_info("%s: Enabling RTC.\n", pdev->name);
507 CMOS_WRITE(OMAP_RTC_CTRL_STOP, OMAP_RTC_CTRL_REG);
509 pr_info("%s: RTC already running.\n", pdev->name);
511 spin_lock_init(&rtc_lock);
512 misc_register(&rtc_dev);
513 create_proc_read_entry("driver/rtc", 0, NULL, rtc_read_proc, NULL);
518 release_resource(mem);
522 static int __exit omap_rtc_remove(struct device *dev)
524 free_irq (omap_rtc_timer, NULL);
525 free_irq (omap_rtc_alarm, NULL);
527 remove_proc_entry ("driver/rtc", NULL);
528 misc_deregister(&rtc_dev);
530 release_resource(dev_get_drvdata(dev));
535 * Info exported via "/proc/driver/rtc".
538 static int rtc_proc_output (char *buf)
540 #define YN(value) ((value) ? "yes" : "no")
549 * There is no way to tell if the luser has the RTC set for local
550 * time or for Universal Standard Time (GMT). Probably local though.
553 "rtc_time\t: %02d:%02d:%02d\n"
554 "rtc_date\t: %04d-%02d-%02d\n"
555 "rtc_epoch\t: %04lu\n",
556 tm.tm_hour, tm.tm_min, tm.tm_sec,
557 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, epoch);
559 get_rtc_alm_time(&tm);
562 * We implicitly assume 24hr mode here. Alarm values >= 0xc0 will
563 * match any value for that particular field. Values that are
564 * greater than a valid time, but less than 0xc0 shouldn't appear.
567 "alarm_time\t: %02d:%02d:%02d\n"
568 "alarm_date\t: %04d-%02d-%02d\n",
569 tm.tm_hour, tm.tm_min, tm.tm_sec,
570 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
577 "update_rate\t: %ud\n",
580 YN(CMOS_READ(OMAP_RTC_INTERRUPTS_REG) &
581 OMAP_RTC_INTERRUPTS_IT_ALARM),
582 YN(CMOS_READ(OMAP_RTC_INTERRUPTS_REG) &
583 OMAP_RTC_INTERRUPTS_IT_TIMER),
584 CMOS_READ(OMAP_RTC_INTERRUPTS_REG) & 3 /* REVISIT */);
590 static int rtc_read_proc(char *page, char **start, off_t off,
591 int count, int *eof, void *data)
593 int len = rtc_proc_output (page);
595 if (len <= off+count)
607 * Returns true if a clock update is in progress
609 static inline unsigned char rtc_is_updating(void)
613 spin_lock_irq(&rtc_lock);
614 uip = (CMOS_READ(OMAP_RTC_STATUS_REG) & OMAP_RTC_STATUS_BUSY);
615 spin_unlock_irq(&rtc_lock);
619 static void bcd2tm(struct rtc_time *tm)
621 BCD_TO_BIN(tm->tm_sec);
622 BCD_TO_BIN(tm->tm_min);
623 BCD_TO_BIN(tm->tm_hour);
624 BCD_TO_BIN(tm->tm_mday);
625 BCD_TO_BIN(tm->tm_mon);
626 BCD_TO_BIN(tm->tm_year);
629 * Account for differences between how the RTC uses the values
630 * and how they are defined in a struct rtc_time;
632 if ((tm->tm_year += (epoch - 1900)) <= 69)
639 static void get_rtc_time(struct rtc_time *rtc_tm)
643 /* REVISIT: Fix this comment!!!
644 * read RTC once any update in progress is done. The update
645 * can take just over 2ms. We wait 10 to 20ms. There is no need to
646 * to poll-wait (up to 1s - eeccch) for the falling edge of OMAP_RTC_STATUS_BUSY.
647 * If you need to know *exactly* when a second has started, enable
648 * periodic update complete interrupts, (via ioctl) and then
649 * immediately read /dev/rtc which will block until you get the IRQ.
650 * Once the read clears, read the RTC time (again via ioctl). Easy.
653 #if 0 /* REVISIT: This need to do as the TRM says. */
654 unsigned long uip_watchdog = jiffies;
655 if (rtc_is_updating() != 0)
656 while (jiffies - uip_watchdog < 2*HZ/100) {
663 * Only the values that we read from the RTC are set. We leave
664 * tm_wday, tm_yday and tm_isdst untouched. Even though the
665 * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
666 * by the RTC when initially set to a non-zero value.
668 spin_lock_irq(&rtc_lock);
669 rtc_tm->tm_sec = CMOS_READ(OMAP_RTC_SECONDS_REG);
670 rtc_tm->tm_min = CMOS_READ(OMAP_RTC_MINUTES_REG);
671 rtc_tm->tm_hour = CMOS_READ(OMAP_RTC_HOURS_REG);
672 rtc_tm->tm_mday = CMOS_READ(OMAP_RTC_DAYS_REG);
673 rtc_tm->tm_mon = CMOS_READ(OMAP_RTC_MONTHS_REG);
674 rtc_tm->tm_year = CMOS_READ(OMAP_RTC_YEARS_REG);
675 ctrl = CMOS_READ(OMAP_RTC_CTRL_REG);
676 spin_unlock_irq(&rtc_lock);
681 static void get_rtc_alm_time(struct rtc_time *alm_tm)
685 spin_lock_irq(&rtc_lock);
686 alm_tm->tm_sec = CMOS_READ(OMAP_RTC_ALARM_SECONDS_REG);
687 alm_tm->tm_min = CMOS_READ(OMAP_RTC_ALARM_MINUTES_REG);
688 alm_tm->tm_hour = CMOS_READ(OMAP_RTC_ALARM_HOURS_REG);
689 alm_tm->tm_mday = CMOS_READ(OMAP_RTC_ALARM_DAYS_REG);
690 alm_tm->tm_mon = CMOS_READ(OMAP_RTC_ALARM_MONTHS_REG);
691 alm_tm->tm_year = CMOS_READ(OMAP_RTC_ALARM_YEARS_REG);
692 ctrl = CMOS_READ(OMAP_RTC_CTRL_REG);
693 spin_unlock_irq(&rtc_lock);
699 * Used to disable/enable UIE and AIE interrupts.
702 static void mask_rtc_irq_bit(unsigned char bit)
706 spin_lock_irq(&rtc_lock);
707 val = CMOS_READ(OMAP_RTC_INTERRUPTS_REG);
709 CMOS_WRITE(val, OMAP_RTC_INTERRUPTS_REG);
711 spin_unlock_irq(&rtc_lock);
714 static void set_rtc_irq_bit(unsigned char bit)
718 spin_lock_irq(&rtc_lock);
719 val = CMOS_READ(OMAP_RTC_INTERRUPTS_REG);
721 CMOS_WRITE(val, OMAP_RTC_INTERRUPTS_REG);
723 spin_unlock_irq(&rtc_lock);
727 static struct timespec rtc_delta;
729 static int rtc_suspend(struct device *dev, pm_message_t state)
731 struct rtc_time rtc_tm;
732 struct timespec time;
735 get_rtc_time(&rtc_tm);
736 rtc_tm_to_time(&rtc_tm, &time.tv_sec);
738 save_time_delta(&rtc_delta, &time);
743 static int rtc_resume(struct device *dev)
745 struct rtc_time rtc_tm;
746 struct timespec time;
749 get_rtc_time(&rtc_tm);
750 rtc_tm_to_time(&rtc_tm, &time.tv_sec);
752 restore_time_delta(&rtc_delta, &time);
757 #define rtc_suspend NULL
758 #define rtc_resume NULL
761 static struct device_driver omap_rtc_driver = {
763 .bus = &platform_bus_type,
764 .probe = omap_rtc_probe,
765 .remove = __exit_p(omap_rtc_remove),
766 .suspend = rtc_suspend,
767 .resume = rtc_resume,
770 static int __init rtc_init(void)
772 return driver_register(&omap_rtc_driver);
775 static void __exit rtc_exit(void)
777 driver_unregister(&omap_rtc_driver);
780 module_init(rtc_init);
781 module_exit(rtc_exit);
783 MODULE_AUTHOR("George G. Davis (and others)");
784 MODULE_LICENSE("GPL");
785 MODULE_ALIAS_MISCDEV(RTC_MINOR);