]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/char/omap-rtc.c
ARM: OMAP: Fix omap-rtc
[linux-2.6-omap-h63xx.git] / drivers / char / omap-rtc.c
1 /*
2  *      TI OMAP Real Time Clock interface for Linux     
3  *
4  *      Copyright (C) 2003 MontaVista Software, Inc.
5  *      Author: George G. Davis <gdavis@mvista.com> or <source@mvista.com>
6  *
7  *      Initially based on linux-2.4.20/drivers/char/rtc.c
8  *      Copyright (C) 1996 Paul Gortmaker
9  *
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.
14  *
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.
18  *
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.
25  *
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.
30  *
31  *      Based on other minimal char device drivers, like Alan's
32  *      watchdog, Ted's random, etc. etc.
33  *
34  * Change Log :
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
38  */
39
40 /*
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.
44  */
45
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>
61
62 #include <asm/io.h>
63 #include <asm/uaccess.h>
64 #include <asm/system.h>
65 #include <asm/hardware.h>
66 #include <asm/irq.h>
67 #include <asm/rtc.h>
68 #include <asm/mach/time.h>
69
70 #include "omap-rtc.h"
71
72 extern spinlock_t rtc_lock;
73
74 static int omap_rtc_alarm = NO_IRQ;
75 static int omap_rtc_timer = NO_IRQ;
76
77
78 /* OMAP RTC register access macros: */
79
80 #define CMOS_READ(addr)         omap_readb(addr)
81 #define CMOS_WRITE(val, addr)   omap_writeb(val, addr)
82
83 static struct fasync_struct *rtc_async_queue;
84
85 static DECLARE_WAIT_QUEUE_HEAD(rtc_wait);
86
87 static void get_rtc_time (struct rtc_time *rtc_tm);
88 static void get_rtc_alm_time (struct rtc_time *alm_tm);
89
90 static void set_rtc_irq_bit(unsigned char bit);
91 static void mask_rtc_irq_bit(unsigned char bit);
92
93 static int rtc_read_proc(char *page, char **start, off_t off,
94                          int count, int *eof, void *data);
95
96 /*
97  *      Bits in rtc_status. (7 bits of room for future expansion)
98  */
99
100 #define RTC_IS_OPEN             0x01    /* means /dev/rtc is in use     */
101
102 /*
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.
106  */
107 static unsigned long rtc_status = 0;    /* bitmapped status byte.       */
108 static unsigned long rtc_irq_data = 0;  /* our output to the world      */
109
110 /*
111  *      If this driver ever becomes modularised, it will be really nice
112  *      to make the epoch retain its value across module reload...
113  */
114
115 static unsigned long epoch = 1900;      /* year corresponding to 0x00   */
116
117 static const unsigned char days_in_mo[] = 
118 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
119
120 /*
121  *      A very tiny interrupt handler. It runs with SA_INTERRUPT set.
122  */
123
124 irqreturn_t rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
125 {
126         /*
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
130          *      of rtc_irq_data.
131          */
132
133         spin_lock (&rtc_lock);
134
135         rtc_irq_data += 0x100;
136         rtc_irq_data &= ~0xff;
137         rtc_irq_data |= CMOS_READ(OMAP_RTC_STATUS_REG);
138
139         if (rtc_irq_data & OMAP_RTC_STATUS_ALARM)
140                 CMOS_WRITE(OMAP_RTC_STATUS_ALARM, OMAP_RTC_STATUS_REG);
141
142         spin_unlock (&rtc_lock);
143
144         /* Now do the rest of the actions */
145         wake_up_interruptible(&rtc_wait);       
146
147         kill_fasync (&rtc_async_queue, SIGIO, POLL_IN);
148         return IRQ_HANDLED;
149 }
150
151 /*
152  *      Now all the various file operations that we export.
153  */
154
155 static ssize_t rtc_read(struct file *file, char __user *buf,
156                         size_t count, loff_t *ppos)
157 {
158         DECLARE_WAITQUEUE(wait, current);
159         unsigned long data;
160         ssize_t retval;
161         
162         if (count < sizeof(unsigned long))
163                 return -EINVAL;
164
165         add_wait_queue(&rtc_wait, &wait);
166         set_current_state(TASK_INTERRUPTIBLE);
167
168         for (;;) {
169                 spin_lock_irq (&rtc_lock);
170                 data = rtc_irq_data;
171                 if (data != 0) {
172                         rtc_irq_data = 0;
173                         break;
174                 }
175                 spin_unlock_irq (&rtc_lock);
176
177                 if (file->f_flags & O_NONBLOCK) {
178                         retval = -EAGAIN;
179                         goto out;
180                 }
181                 if (signal_pending(current)) {
182                         retval = -ERESTARTSYS;
183                         goto out;
184                 }
185                 schedule();
186         }
187
188         spin_unlock_irq (&rtc_lock);
189         retval = put_user(data, (unsigned long __user *)buf);
190         if (!retval)
191                 retval = sizeof(unsigned long); 
192  out:
193         set_current_state(TASK_RUNNING);
194         remove_wait_queue(&rtc_wait, &wait);
195
196         return retval;
197 }
198
199 /* convert from userspace struct to hardware BCD-encoded version,
200  * or return error code
201  */
202 static int utm2bcd(struct rtc_time __user *arg, struct rtc_time *tm)
203 {
204         unsigned char leap_yr;
205
206         if (copy_from_user(tm, arg, sizeof(struct rtc_time)))
207                 return -EFAULT;
208
209         tm->tm_year += 1900;
210         tm->tm_mon++;
211
212         if (tm->tm_year < 1970)
213                 return -EINVAL;
214
215         leap_yr = (!(tm->tm_year % 4) && (tm->tm_year % 100))
216                         || !(tm->tm_year % 400);
217
218         if ((tm->tm_mon > 12) || (tm->tm_mday == 0))
219                 return -EINVAL;
220
221         if (tm->tm_mday > (days_in_mo[tm->tm_mon] + ((tm->tm_mon == 2) && leap_yr)))
222                 return -EINVAL;
223
224         if ((tm->tm_hour >= 24) || (tm->tm_min >= 60) || (tm->tm_sec >= 60))
225                 return -EINVAL;
226
227         if ((tm->tm_year -= epoch) > 255)    /* They are unsigned */
228                 return -EINVAL;
229
230         if (tm->tm_year > 169)
231                 return -EINVAL;
232
233         if (tm->tm_year >= 100)
234                 tm->tm_year -= 100;
235
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);
242
243         return 0;
244 }
245
246 static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
247                      unsigned long arg)
248 {
249         struct rtc_time wtime; 
250         int status = 0;
251         u8 save_control;
252
253         switch (cmd) {
254         case RTC_AIE_OFF:       /* Mask alarm int. enab. bit    */
255                 mask_rtc_irq_bit(OMAP_RTC_INTERRUPTS_IT_ALARM);
256                 break;
257         case RTC_AIE_ON:        /* Allow alarm interrupts.      */
258                 set_rtc_irq_bit(OMAP_RTC_INTERRUPTS_IT_ALARM);
259                 break;
260         case RTC_UIE_OFF:       /* Mask ints from RTC updates.  */
261                 mask_rtc_irq_bit(OMAP_RTC_INTERRUPTS_IT_TIMER);
262                 break;
263         case RTC_UIE_ON:        /* Allow ints for RTC updates.  */
264                 set_rtc_irq_bit(OMAP_RTC_INTERRUPTS_IT_TIMER);
265                 break;
266         case RTC_ALM_READ:      /* Read the present alarm time */
267                 /*
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.
271                  */
272                 memset(&wtime, 0, sizeof(struct rtc_time));
273                 get_rtc_alm_time(&wtime);
274                 goto return_wtime;
275         case RTC_ALM_SET:       /* Store a time into the alarm */
276                 status = utm2bcd((void __user *)arg, &wtime);
277                 if (status != 0)
278                         return status;
279
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);
288
289                 break;
290         case RTC_RD_TIME:       /* Read the time/date from RTC  */
291                 memset(&wtime, 0, sizeof(struct rtc_time));
292                 get_rtc_time(&wtime);
293                 goto return_wtime;
294         case RTC_SET_TIME:      /* Set the RTC */
295                 if (!capable(CAP_SYS_TIME))
296                         return -EACCES;
297
298                 status = utm2bcd((void __user *)arg, &wtime);
299                 if (status != 0)
300                         return status;
301
302                 spin_lock_irq(&rtc_lock);
303                 save_control = CMOS_READ(OMAP_RTC_CTRL_REG);
304                 CMOS_WRITE((save_control & ~OMAP_RTC_CTRL_STOP),
305                            OMAP_RTC_CTRL_REG);
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),
313                            OMAP_RTC_CTRL_REG);
314                 spin_unlock_irq(&rtc_lock);
315
316                 break;
317         case RTC_EPOCH_READ:    /* Read the epoch.      */
318                 status = put_user (epoch, (unsigned long  __user *)arg);
319                 break;
320         case RTC_EPOCH_SET:     /* Set the epoch.       */
321                 if (!capable(CAP_SYS_TIME))
322                         return -EACCES;
323
324                 /* 
325                  * There were no RTC clocks before 1900.
326                  */
327                 if (arg < 1900)
328                         status = -EINVAL;
329                 else
330                         epoch = arg;
331                 break;
332         default:
333                 status = -ENOTTY;
334         }
335         return status;
336
337 return_wtime:
338         return copy_to_user((void  __user *)arg, &wtime, sizeof wtime)
339                 ? -EFAULT
340                 : 0;
341 }
342
343 /*
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.
347  */
348
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)
352 {
353         spin_lock_irq (&rtc_lock);
354
355         if (rtc_status & RTC_IS_OPEN)
356                 goto out_busy;
357
358         rtc_status |= RTC_IS_OPEN;
359
360         rtc_irq_data = 0;
361         spin_unlock_irq (&rtc_lock);
362         return 0;
363
364 out_busy:
365         spin_unlock_irq (&rtc_lock);
366         return -EBUSY;
367 }
368
369 static int rtc_fasync(int fd, struct file *filp, int on)
370 {
371         return fasync_helper (fd, filp, on, &rtc_async_queue);
372 }
373
374 static int rtc_release(struct inode *inode, struct file *file)
375 {
376         unsigned char tmp;
377
378         /*
379          * Turn off all interrupts once the device is no longer
380          * in use, and clear the data.
381          */
382
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);
389
390         if (file->f_flags & FASYNC) {
391                 rtc_fasync (-1, file, 0);
392         }
393
394         spin_lock_irq (&rtc_lock);
395         rtc_irq_data = 0;
396         spin_unlock_irq (&rtc_lock);
397
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.
400          */
401         rtc_status &= ~RTC_IS_OPEN;
402         return 0;
403 }
404
405 /* Called without the kernel lock - fine */
406 static unsigned int rtc_poll(struct file *file, poll_table *wait)
407 {
408         unsigned long l;
409
410         poll_wait(file, &rtc_wait, wait);
411
412         spin_lock_irq (&rtc_lock);
413         l = rtc_irq_data;
414         spin_unlock_irq (&rtc_lock);
415
416         if (l != 0)
417                 return POLLIN | POLLRDNORM;
418         return 0;
419 }
420
421 /*
422  *      The various file operations we support.
423  */
424
425 static struct file_operations rtc_fops = {
426         .owner          = THIS_MODULE,
427         .llseek         = no_llseek,
428         .read           = rtc_read,
429         .poll           = rtc_poll,
430         .ioctl          = rtc_ioctl,
431         .open           = rtc_open,
432         .release        = rtc_release,
433         .fasync         = rtc_fasync,
434 };
435
436 static struct miscdevice rtc_dev = {
437         .minor          = RTC_MINOR,
438         .name           = "rtc",
439         .fops           = &rtc_fops,
440 };
441
442 static int __init omap_rtc_probe(struct platform_device *pdev)
443 {
444         struct resource         *res, *mem;
445
446         /* find the IRQs */
447
448         omap_rtc_timer = platform_get_irq(pdev, 0);
449         if (omap_rtc_timer <= 0) {
450                 dev_err(&pdev->dev, "no irq for rtc timer\n");
451                 return -ENOENT;
452         }
453
454         omap_rtc_alarm = platform_get_irq(pdev, 1);
455         if (omap_rtc_alarm <= 0) {
456                 dev_err(&pdev->dev, "no irq for alarm\n");
457                 return -ENOENT;
458         }
459
460         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
461         if (res)
462                 mem = request_mem_region(res->start,
463                                 res->end - res->start + 1,
464                                 pdev->name);
465         else
466                 mem = NULL;
467         if (!mem) {
468                 pr_debug("%s: RTC registers at %x are not free.\n",
469                         pdev->name, OMAP_RTC_BASE);
470                 return -EBUSY;
471         }
472         platform_set_drvdata(pdev, mem);
473
474         if (CMOS_READ(OMAP_RTC_STATUS_REG) & OMAP_RTC_STATUS_POWER_UP) {
475                 pr_info("%s: RTC power up reset detected.\n",
476                         pdev->name);
477                 /* Clear OMAP_RTC_STATUS_POWER_UP */
478                 CMOS_WRITE(OMAP_RTC_STATUS_POWER_UP, OMAP_RTC_STATUS_REG);
479         }
480
481         if (CMOS_READ(OMAP_RTC_STATUS_REG) & OMAP_RTC_STATUS_ALARM) {
482                 pr_debug("%s: Clearing RTC ALARM interrupt.\n",
483                         pdev->name);
484                 /* Clear OMAP_RTC_STATUS_ALARM */
485                 CMOS_WRITE(OMAP_RTC_STATUS_ALARM, OMAP_RTC_STATUS_REG);
486         }
487
488         if (request_irq(omap_rtc_timer, rtc_interrupt, SA_INTERRUPT,
489                         pdev->name, NULL)) {
490                 pr_debug("%s: RTC timer interrupt IRQ%d is not free.\n",
491                         pdev->name, omap_rtc_timer);
492                 goto fail;
493         }
494
495         if (request_irq(omap_rtc_alarm, rtc_interrupt, SA_INTERRUPT,
496                         pdev->name, NULL)) {
497                 pr_debug("%s: RTC alarm interrupt IRQ%d is not free.\n",
498                         pdev->name, omap_rtc_alarm);
499                 free_irq(omap_rtc_timer, NULL);
500                 goto fail;
501         }
502
503         /* On boards with split power, RTC_ON_NOFF resets all but the RTC */
504         if (!(CMOS_READ(OMAP_RTC_CTRL_REG) & OMAP_RTC_CTRL_STOP)) {
505                 pr_info("%s: Enabling RTC.\n", pdev->name);
506                 CMOS_WRITE(OMAP_RTC_CTRL_STOP, OMAP_RTC_CTRL_REG);
507         } else
508                 pr_info("%s: RTC already running.\n", pdev->name);
509
510         spin_lock_init(&rtc_lock);
511         misc_register(&rtc_dev);
512         create_proc_read_entry("driver/rtc", 0, NULL, rtc_read_proc, NULL);
513
514         return 0;
515
516 fail:
517         release_resource(mem);
518         return -EIO;
519 }
520
521 static int omap_rtc_remove(struct platform_device *pdev)
522 {
523         free_irq (omap_rtc_timer, NULL);
524         free_irq (omap_rtc_alarm, NULL);
525
526         remove_proc_entry ("driver/rtc", NULL);
527         misc_deregister(&rtc_dev);
528
529         release_resource(platform_get_drvdata(pdev));
530         return 0;
531 }
532
533 /*
534  *      Info exported via "/proc/driver/rtc".
535  */
536
537 static int rtc_proc_output (char *buf)
538 {
539 #define YN(value) ((value) ? "yes" : "no")
540         char *p;
541         struct rtc_time tm;
542
543         p = buf;
544
545         get_rtc_time(&tm);
546
547         /*
548          * There is no way to tell if the luser has the RTC set for local
549          * time or for Universal Standard Time (GMT). Probably local though.
550          */
551         p += sprintf(p,
552                      "rtc_time\t: %02d:%02d:%02d\n"
553                      "rtc_date\t: %04d-%02d-%02d\n"
554                      "rtc_epoch\t: %04lu\n",
555                      tm.tm_hour, tm.tm_min, tm.tm_sec,
556                      tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, epoch);
557
558         get_rtc_alm_time(&tm);
559
560         /*
561          * We implicitly assume 24hr mode here. Alarm values >= 0xc0 will
562          * match any value for that particular field. Values that are
563          * greater than a valid time, but less than 0xc0 shouldn't appear.
564          */
565         p += sprintf(p,
566                      "alarm_time\t: %02d:%02d:%02d\n"
567                      "alarm_date\t: %04d-%02d-%02d\n",
568                      tm.tm_hour, tm.tm_min, tm.tm_sec,
569                      tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
570
571         p += sprintf(p,
572                      "BCD\t\t: %s\n"
573                      "24hr\t\t: %s\n"
574                      "alarm_IRQ\t: %s\n"
575                      "update_IRQ\t: %s\n"
576                      "update_rate\t: %ud\n",
577                      YN(1),
578                      YN(1),
579                      YN(CMOS_READ(OMAP_RTC_INTERRUPTS_REG) &
580                         OMAP_RTC_INTERRUPTS_IT_ALARM),
581                      YN(CMOS_READ(OMAP_RTC_INTERRUPTS_REG) &
582                         OMAP_RTC_INTERRUPTS_IT_TIMER),
583                      CMOS_READ(OMAP_RTC_INTERRUPTS_REG) & 3 /* REVISIT */);
584
585         return  p - buf;
586 #undef YN
587 }
588
589 static int rtc_read_proc(char *page, char **start, off_t off,
590                          int count, int *eof, void *data)
591 {
592         int len = rtc_proc_output (page);
593
594         if (len <= off+count)
595                 *eof = 1;
596         *start = page + off;
597         len -= off;
598         if (len > count)
599                 len = count;
600         if (len < 0)
601                 len = 0;
602         return len;
603 }
604
605 /*
606  * Returns true if a clock update is in progress
607  */
608 static inline unsigned char rtc_is_updating(void)
609 {
610         unsigned char uip;
611
612         spin_lock_irq(&rtc_lock);
613         uip = (CMOS_READ(OMAP_RTC_STATUS_REG) & OMAP_RTC_STATUS_BUSY);
614         spin_unlock_irq(&rtc_lock);
615         return uip;
616 }
617
618 static void bcd2tm(struct rtc_time *tm)
619 {
620         BCD_TO_BIN(tm->tm_sec);
621         BCD_TO_BIN(tm->tm_min);
622         BCD_TO_BIN(tm->tm_hour);
623         BCD_TO_BIN(tm->tm_mday);
624         BCD_TO_BIN(tm->tm_mon);
625         BCD_TO_BIN(tm->tm_year);
626
627         /*
628          * Account for differences between how the RTC uses the values
629          * and how they are defined in a struct rtc_time;
630          */
631         if ((tm->tm_year += (epoch - 1900)) <= 69)
632                 tm->tm_year += 100;
633
634         tm->tm_mon--;
635 }
636
637
638 static void get_rtc_time(struct rtc_time *rtc_tm)
639 {
640         unsigned char ctrl;
641
642         /* REVISIT: Fix this comment!!!
643          * read RTC once any update in progress is done. The update
644          * can take just over 2ms. We wait 10 to 20ms. There is no need to
645          * to poll-wait (up to 1s - eeccch) for the falling edge of OMAP_RTC_STATUS_BUSY.
646          * If you need to know *exactly* when a second has started, enable
647          * periodic update complete interrupts, (via ioctl) and then 
648          * immediately read /dev/rtc which will block until you get the IRQ.
649          * Once the read clears, read the RTC time (again via ioctl). Easy.
650          */
651
652 #if     0 /* REVISIT: This need to do as the TRM says. */
653         unsigned long uip_watchdog = jiffies;
654         if (rtc_is_updating() != 0)
655                 while (jiffies - uip_watchdog < 2*HZ/100) {
656                         barrier();
657                         cpu_relax();
658                 }
659 #endif
660
661         /*
662          * Only the values that we read from the RTC are set. We leave
663          * tm_wday, tm_yday and tm_isdst untouched. Even though the
664          * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
665          * by the RTC when initially set to a non-zero value.
666          */
667         spin_lock_irq(&rtc_lock);
668         rtc_tm->tm_sec = CMOS_READ(OMAP_RTC_SECONDS_REG);
669         rtc_tm->tm_min = CMOS_READ(OMAP_RTC_MINUTES_REG);
670         rtc_tm->tm_hour = CMOS_READ(OMAP_RTC_HOURS_REG);
671         rtc_tm->tm_mday = CMOS_READ(OMAP_RTC_DAYS_REG);
672         rtc_tm->tm_mon = CMOS_READ(OMAP_RTC_MONTHS_REG);
673         rtc_tm->tm_year = CMOS_READ(OMAP_RTC_YEARS_REG);
674         ctrl = CMOS_READ(OMAP_RTC_CTRL_REG);
675         spin_unlock_irq(&rtc_lock);
676
677         bcd2tm(rtc_tm);
678 }
679
680 static void get_rtc_alm_time(struct rtc_time *alm_tm)
681 {
682         unsigned char ctrl;
683
684         spin_lock_irq(&rtc_lock);
685         alm_tm->tm_sec = CMOS_READ(OMAP_RTC_ALARM_SECONDS_REG);
686         alm_tm->tm_min = CMOS_READ(OMAP_RTC_ALARM_MINUTES_REG);
687         alm_tm->tm_hour = CMOS_READ(OMAP_RTC_ALARM_HOURS_REG);
688         alm_tm->tm_mday = CMOS_READ(OMAP_RTC_ALARM_DAYS_REG);
689         alm_tm->tm_mon = CMOS_READ(OMAP_RTC_ALARM_MONTHS_REG);
690         alm_tm->tm_year = CMOS_READ(OMAP_RTC_ALARM_YEARS_REG);
691         ctrl = CMOS_READ(OMAP_RTC_CTRL_REG);
692         spin_unlock_irq(&rtc_lock);
693
694         bcd2tm(alm_tm);
695 }
696
697 /*
698  * Used to disable/enable UIE and AIE interrupts.
699  */
700
701 static void mask_rtc_irq_bit(unsigned char bit)
702 {
703         unsigned char val;
704
705         spin_lock_irq(&rtc_lock);
706         val = CMOS_READ(OMAP_RTC_INTERRUPTS_REG);
707         val &=  ~bit;
708         CMOS_WRITE(val, OMAP_RTC_INTERRUPTS_REG);
709         rtc_irq_data = 0;
710         spin_unlock_irq(&rtc_lock);
711 }
712
713 static void set_rtc_irq_bit(unsigned char bit)
714 {
715         unsigned char val;
716
717         spin_lock_irq(&rtc_lock);
718         val = CMOS_READ(OMAP_RTC_INTERRUPTS_REG);
719         val |= bit;
720         CMOS_WRITE(val, OMAP_RTC_INTERRUPTS_REG);
721         rtc_irq_data = 0;
722         spin_unlock_irq(&rtc_lock);
723 }
724
725 #ifdef CONFIG_PM
726 static struct timespec rtc_delta;
727
728 static int omap_rtc_suspend(struct platform_device *pdev, pm_message_t state)
729 {
730         struct rtc_time rtc_tm;
731         struct timespec time;
732
733         time.tv_nsec = 0;
734         get_rtc_time(&rtc_tm);
735         rtc_tm_to_time(&rtc_tm, &time.tv_sec);
736
737         save_time_delta(&rtc_delta, &time);
738
739         return 0;
740 }
741
742 static int omap_rtc_resume(struct platform_device *pdev)
743 {
744         struct rtc_time rtc_tm;
745         struct timespec time;
746
747         time.tv_nsec = 0;
748         get_rtc_time(&rtc_tm);
749         rtc_tm_to_time(&rtc_tm, &time.tv_sec);
750
751         restore_time_delta(&rtc_delta, &time);
752
753         return 0;
754 }
755 #else
756 #define omap_rtc_suspend NULL
757 #define omap_rtc_resume  NULL
758 #endif
759
760 static struct platform_driver omap_rtc_driver = {
761         .probe          = omap_rtc_probe,
762         .remove         = omap_rtc_remove,
763         .suspend        = omap_rtc_suspend,
764         .resume         = omap_rtc_resume,
765         .driver         = {
766                 .name   = "omap_rtc",
767                 .owner  = THIS_MODULE,
768         },
769 };
770
771 static char __initdata banner[] = KERN_INFO "OMAP RTC Driver\n";
772
773 static int __init rtc_init(void)
774 {
775         printk(banner);
776         return platform_driver_register(&omap_rtc_driver);
777 }
778
779 static void __exit rtc_exit(void)
780 {
781         platform_driver_unregister(&omap_rtc_driver);
782 }
783
784 module_init(rtc_init);
785 module_exit(rtc_exit);
786
787 MODULE_AUTHOR("George G. Davis (and others)");
788 MODULE_LICENSE("GPL");
789 MODULE_ALIAS_MISCDEV(RTC_MINOR);