]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/char/omap-rtc.c
Merge with mainline
[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 #define RTC_VERSION             "1.0"
41
42 /*
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.
46  */
47
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>
60
61 #include <linux/interrupt.h>
62 #include <linux/rtc.h>
63
64 #include <asm/io.h>
65 #include <asm/uaccess.h>
66 #include <asm/system.h>
67 #include <asm/hardware.h>
68 #include <asm/irq.h>
69 #include "omap-rtc.h"
70
71 extern spinlock_t rtc_lock;
72
73
74 /* OMAP RTC register access macros: */
75
76 #define CMOS_READ(addr)         omap_readb(addr)
77 #define CMOS_WRITE(val, addr)   omap_writeb(val, addr)
78
79
80 /* Local BCD/BIN conversion macros: */
81 #ifdef BCD_TO_BIN
82 #undef BCD_TO_BIN
83 #endif
84 #define BCD_TO_BIN(val) ((val)=((val)&15) + ((val)>>4)*10)
85  
86 #ifdef BIN_TO_BCD
87 #undef BIN_TO_BCD
88 #endif
89 #define BIN_TO_BCD(val) ((val)=(((val)/10)<<4) + (val)%10)
90
91
92 /*
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
96  *      ioctls.
97  */
98
99 static struct fasync_struct *rtc_async_queue;
100
101 static DECLARE_WAIT_QUEUE_HEAD(rtc_wait);
102
103 static ssize_t rtc_read(struct file *file, char *buf,
104                         size_t count, loff_t *ppos);
105
106 static int rtc_ioctl(struct inode *inode, struct file *file,
107                      unsigned int cmd, unsigned long arg);
108
109 static unsigned int rtc_poll(struct file *file, poll_table *wait);
110
111 static void get_rtc_time (struct rtc_time *rtc_tm);
112 static void get_rtc_alm_time (struct rtc_time *alm_tm);
113
114 static void set_rtc_irq_bit(unsigned char bit);
115 static void mask_rtc_irq_bit(unsigned char bit);
116
117 static inline unsigned char rtc_is_updating(void);
118
119 static int rtc_read_proc(char *page, char **start, off_t off,
120                          int count, int *eof, void *data);
121
122 /*
123  *      Bits in rtc_status. (7 bits of room for future expansion)
124  */
125
126 #define RTC_IS_OPEN             0x01    /* means /dev/rtc is in use     */
127
128 /*
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.
132  */
133 static unsigned long rtc_status = 0;    /* bitmapped status byte.       */
134 static unsigned long rtc_irq_data = 0;  /* our output to the world      */
135
136 /*
137  *      If this driver ever becomes modularised, it will be really nice
138  *      to make the epoch retain its value across module reload...
139  */
140
141 static unsigned long epoch = 1900;      /* year corresponding to 0x00   */
142
143 static const unsigned char days_in_mo[] = 
144 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
145
146 /*
147  *      A very tiny interrupt handler. It runs with SA_INTERRUPT set.
148  */
149
150 static irqreturn_t rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
151 {
152         /*
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
156          *      of rtc_irq_data.
157          */
158
159         spin_lock (&rtc_lock);
160
161         rtc_irq_data += 0x100;
162         rtc_irq_data &= ~0xff;
163         rtc_irq_data |= CMOS_READ(OMAP_RTC_STATUS_REG);
164
165         if (rtc_irq_data & OMAP_RTC_STATUS_ALARM)
166                 CMOS_WRITE(OMAP_RTC_STATUS_ALARM, OMAP_RTC_STATUS_REG);
167
168         spin_unlock (&rtc_lock);
169
170         /* Now do the rest of the actions */
171         wake_up_interruptible(&rtc_wait);       
172
173         kill_fasync (&rtc_async_queue, SIGIO, POLL_IN);
174         return IRQ_HANDLED;
175 }
176
177 /*
178  *      Now all the various file operations that we export.
179  */
180
181 static ssize_t rtc_read(struct file *file, char *buf,
182                         size_t count, loff_t *ppos)
183 {
184         DECLARE_WAITQUEUE(wait, current);
185         unsigned long data;
186         ssize_t retval;
187         
188         if (count < sizeof(unsigned long))
189                 return -EINVAL;
190
191         add_wait_queue(&rtc_wait, &wait);
192         set_current_state(TASK_INTERRUPTIBLE);
193
194         for (;;) {
195                 spin_lock_irq (&rtc_lock);
196                 data = rtc_irq_data;
197                 if (data != 0) {
198                         rtc_irq_data = 0;
199                         break;
200                 }
201                 spin_unlock_irq (&rtc_lock);
202
203                 if (file->f_flags & O_NONBLOCK) {
204                         retval = -EAGAIN;
205                         goto out;
206                 }
207                 if (signal_pending(current)) {
208                         retval = -ERESTARTSYS;
209                         goto out;
210                 }
211                 schedule();
212         }
213
214         spin_unlock_irq (&rtc_lock);
215         retval = put_user(data, (unsigned long *)buf); 
216         if (!retval)
217                 retval = sizeof(unsigned long); 
218  out:
219         set_current_state(TASK_RUNNING);
220         remove_wait_queue(&rtc_wait, &wait);
221
222         return retval;
223 }
224
225 static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
226                      unsigned long arg)
227 {
228         struct rtc_time wtime; 
229
230         switch (cmd) {
231         case RTC_AIE_OFF:       /* Mask alarm int. enab. bit    */
232         {
233                 mask_rtc_irq_bit(OMAP_RTC_INTERRUPTS_IT_ALARM);
234                 return 0;
235         }
236         case RTC_AIE_ON:        /* Allow alarm interrupts.      */
237         {
238                 set_rtc_irq_bit(OMAP_RTC_INTERRUPTS_IT_ALARM);
239                 return 0;
240         }
241         case RTC_UIE_OFF:       /* Mask ints from RTC updates.  */
242         {
243                 mask_rtc_irq_bit(OMAP_RTC_INTERRUPTS_IT_TIMER);
244                 return 0;
245         }
246         case RTC_UIE_ON:        /* Allow ints for RTC updates.  */
247         {
248                 set_rtc_irq_bit(OMAP_RTC_INTERRUPTS_IT_TIMER);
249                 return 0;
250         }
251         case RTC_ALM_READ:      /* Read the present alarm time */
252         {
253                 /*
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.
257                  */
258                 memset(&wtime, 0, sizeof(struct rtc_time));
259                 get_rtc_alm_time(&wtime);
260                 break; 
261         }
262         case RTC_ALM_SET:       /* Store a time into the alarm */
263         {
264                 struct rtc_time alm_tm;
265                 unsigned char mon, day, hrs, min, sec, leap_yr;
266                 unsigned int yrs;
267
268                 if (copy_from_user(&alm_tm, (struct rtc_time*)arg,
269                                    sizeof(struct rtc_time)))
270                         return -EFAULT;
271
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;
276                 min = alm_tm.tm_min;
277                 sec = alm_tm.tm_sec;
278
279                 if (yrs < 1970)
280                         return -EINVAL;
281
282                 leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
283
284                 if ((mon > 12) || (day == 0))
285                         return -EINVAL;
286
287                 if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
288                         return -EINVAL;
289                         
290                 if ((hrs >= 24) || (min >= 60) || (sec >= 60))
291                         return -EINVAL;
292
293                 if ((yrs -= epoch) > 255)    /* They are unsigned */
294                         return -EINVAL;
295
296                 if (yrs > 169) {
297                         return -EINVAL;
298                 }
299
300                 if (yrs >= 100)
301                         yrs -= 100;
302
303                 BIN_TO_BCD(sec);
304                 BIN_TO_BCD(min);
305                 BIN_TO_BCD(hrs);
306                 BIN_TO_BCD(day);
307                 BIN_TO_BCD(mon);
308                 BIN_TO_BCD(yrs);
309
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);
318
319                 return 0;
320         }
321         case RTC_RD_TIME:       /* Read the time/date from RTC  */
322         {
323                 memset(&wtime, 0, sizeof(struct rtc_time));
324                 get_rtc_time(&wtime);
325                 break;
326         }
327         case RTC_SET_TIME:      /* Set the RTC */
328         {
329                 struct rtc_time rtc_tm;
330                 unsigned char mon, day, hrs, min, sec, leap_yr;
331                 unsigned char save_control;
332                 unsigned int yrs;
333
334                 if (!capable(CAP_SYS_TIME))
335                         return -EACCES;
336
337                 if (copy_from_user(&rtc_tm, (struct rtc_time*)arg,
338                                    sizeof(struct rtc_time)))
339                         return -EFAULT;
340
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;
345                 min = rtc_tm.tm_min;
346                 sec = rtc_tm.tm_sec;
347
348                 if (yrs < 1970)
349                         return -EINVAL;
350
351                 leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
352
353                 if ((mon > 12) || (day == 0))
354                         return -EINVAL;
355
356                 if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
357                         return -EINVAL;
358
359                 if ((hrs >= 24) || (min >= 60) || (sec >= 60))
360                         return -EINVAL;
361
362                 if ((yrs -= epoch) > 255)    /* They are unsigned */
363                         return -EINVAL;
364
365                 if (yrs > 169) {
366                         return -EINVAL;
367                 }
368
369                 if (yrs >= 100)
370                         yrs -= 100;
371
372                 BIN_TO_BCD(sec);
373                 BIN_TO_BCD(min);
374                 BIN_TO_BCD(hrs);
375                 BIN_TO_BCD(day);
376                 BIN_TO_BCD(mon);
377                 BIN_TO_BCD(yrs);
378
379                 spin_lock_irq(&rtc_lock);
380                 save_control = CMOS_READ(OMAP_RTC_CTRL_REG);
381                 CMOS_WRITE((save_control & ~OMAP_RTC_CTRL_STOP),
382                            OMAP_RTC_CTRL_REG);
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),
390                            OMAP_RTC_CTRL_REG);
391                 spin_unlock_irq(&rtc_lock);
392
393                 return 0;
394         }
395         case RTC_EPOCH_READ:    /* Read the epoch.      */
396         {
397                 return put_user (epoch, (unsigned long *)arg);
398         }
399         case RTC_EPOCH_SET:     /* Set the epoch.       */
400         {
401                 /* 
402                  * There were no RTC clocks before 1900.
403                  */
404                 if (arg < 1900)
405                         return -EINVAL;
406
407                 if (!capable(CAP_SYS_TIME))
408                         return -EACCES;
409
410                 epoch = arg;
411                 return 0;
412         }
413         default:
414 #if     !defined(CONFIG_ARCH_OMAP)
415                 return -ENOTTY;
416 #else
417                 return -EINVAL;
418 #endif
419         }
420         return copy_to_user((void *)arg, &wtime, sizeof wtime) ? -EFAULT : 0;
421 }
422
423 /*
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.
427  */
428
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)
432 {
433         spin_lock_irq (&rtc_lock);
434
435         if(rtc_status & RTC_IS_OPEN)
436                 goto out_busy;
437
438         rtc_status |= RTC_IS_OPEN;
439
440         rtc_irq_data = 0;
441         spin_unlock_irq (&rtc_lock);
442         return 0;
443
444 out_busy:
445         spin_unlock_irq (&rtc_lock);
446         return -EBUSY;
447 }
448
449 static int rtc_fasync (int fd, struct file *filp, int on)
450
451 {
452         return fasync_helper (fd, filp, on, &rtc_async_queue);
453 }
454
455 static int rtc_release(struct inode *inode, struct file *file)
456 {
457         unsigned char tmp;
458
459         /*
460          * Turn off all interrupts once the device is no longer
461          * in use, and clear the data.
462          */
463
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);
470
471         if (file->f_flags & FASYNC) {
472                 rtc_fasync (-1, file, 0);
473         }
474
475         spin_lock_irq (&rtc_lock);
476         rtc_irq_data = 0;
477         spin_unlock_irq (&rtc_lock);
478
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.
481          */
482         rtc_status &= ~RTC_IS_OPEN;
483         return 0;
484 }
485
486 /* Called without the kernel lock - fine */
487 static unsigned int rtc_poll(struct file *file, poll_table *wait)
488 {
489         unsigned long l;
490
491         poll_wait(file, &rtc_wait, wait);
492
493         spin_lock_irq (&rtc_lock);
494         l = rtc_irq_data;
495         spin_unlock_irq (&rtc_lock);
496
497         if (l != 0)
498                 return POLLIN | POLLRDNORM;
499         return 0;
500 }
501
502 /*
503  *      The various file operations we support.
504  */
505
506 static struct file_operations rtc_fops = {
507         owner:          THIS_MODULE,
508         llseek:         no_llseek,
509         read:           rtc_read,
510         poll:           rtc_poll,
511         ioctl:          rtc_ioctl,
512         open:           rtc_open,
513         release:        rtc_release,
514         fasync:         rtc_fasync,
515 };
516
517 static struct miscdevice rtc_dev=
518 {
519         RTC_MINOR,
520         "rtc",
521         &rtc_fops
522 };
523
524 static int __init rtc_init(void)
525 {
526         if (!request_region(OMAP_RTC_VIRT_BASE, OMAP_RTC_SIZE,
527                             rtc_dev.name)) {
528                 printk(KERN_ERR "%s: RTC I/O port %d is not free.\n",
529                        rtc_dev.name, OMAP_RTC_VIRT_BASE);
530                 return -EIO;
531         }
532
533         if (CMOS_READ(OMAP_RTC_STATUS_REG) & OMAP_RTC_STATUS_POWER_UP) {
534                 printk(KERN_WARNING "%s: RTC power up reset detected.\n",
535                        rtc_dev.name);
536                 /* Clear OMAP_RTC_STATUS_POWER_UP */
537                 CMOS_WRITE(OMAP_RTC_STATUS_POWER_UP, OMAP_RTC_STATUS_REG);
538         }
539
540         if (CMOS_READ(OMAP_RTC_STATUS_REG) & OMAP_RTC_STATUS_ALARM) {
541                 printk(KERN_WARNING "%s: Clearing RTC ALARM interrupt.\n",
542                        rtc_dev.name);
543                 /* Clear OMAP_RTC_STATUS_ALARM */
544                 CMOS_WRITE(OMAP_RTC_STATUS_ALARM, OMAP_RTC_STATUS_REG);
545         }
546
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);
550         }
551
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);
557                 return -EIO;
558         }
559
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);
565                 return -EIO;
566         }
567
568         spin_lock_init(&rtc_lock);
569         misc_register(&rtc_dev);
570         create_proc_read_entry ("driver/rtc", 0, 0, rtc_read_proc, NULL);
571
572         printk(KERN_INFO "Real Time Clock Driver v" RTC_VERSION "\n");
573
574         return 0;
575 }
576
577 static void __exit rtc_exit (void)
578 {
579         free_irq (INT_RTC_TIMER, NULL);
580         free_irq (INT_RTC_ALARM, NULL);
581
582         remove_proc_entry ("driver/rtc", NULL);
583         misc_deregister(&rtc_dev);
584
585         release_region (OMAP_RTC_VIRT_BASE, OMAP_RTC_SIZE);
586 }
587
588 /*
589  *      Info exported via "/proc/driver/rtc".
590  */
591
592 static int rtc_proc_output (char *buf)
593 {
594 #define YN(value) ((value) ? "yes" : "no")
595 #define NY(value) ((value) ? "no" : "yes")
596         char *p;
597         struct rtc_time tm;
598
599         p = buf;
600
601         get_rtc_time(&tm);
602
603         /*
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.
606          */
607         p += sprintf(p,
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);
613
614         get_rtc_alm_time(&tm);
615
616         /*
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.
620          */
621         p += sprintf(p,
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);
626
627         p += sprintf(p,
628                      "BCD\t\t: %s\n"
629                      "24hr\t\t: %s\n"
630                      "alarm_IRQ\t: %s\n"
631                      "update_IRQ\t: %s\n"
632                      "update_rate\t: %ud\n",
633                      YN(1),
634                      YN(1),
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 */);
640
641         return  p - buf;
642 #undef YN
643 #undef NY
644 }
645
646 static int rtc_read_proc(char *page, char **start, off_t off,
647                          int count, int *eof, void *data)
648 {
649         int len = rtc_proc_output (page);
650         if (len <= off+count) *eof = 1;
651         *start = page + off;
652         len -= off;
653         if (len>count) len = count;
654         if (len<0) len = 0;
655         return len;
656 }
657
658 /*
659  * Returns true if a clock update is in progress
660  */
661 /* FIXME shouldn't this be above rtc_init to make it fully inlined? */
662 static inline unsigned char rtc_is_updating(void)
663 {
664         unsigned char uip;
665
666         spin_lock_irq(&rtc_lock);
667         uip = (CMOS_READ(OMAP_RTC_STATUS_REG) & OMAP_RTC_STATUS_BUSY);
668         spin_unlock_irq(&rtc_lock);
669         return uip;
670 }
671
672 static void get_rtc_time(struct rtc_time *rtc_tm)
673 {
674         unsigned char ctrl;
675
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.
684          */
685
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) {
690                         barrier();
691                         cpu_relax();
692                 }
693 #endif
694
695         /*
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.
700          */
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);
710
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);
717
718         /*
719          * Account for differences between how the RTC uses the values
720          * and how they are defined in a struct rtc_time;
721          */
722         if ((rtc_tm->tm_year += (epoch - 1900)) <= 69)
723                 rtc_tm->tm_year += 100;
724
725         rtc_tm->tm_mon--;
726 }
727
728 static void get_rtc_alm_time(struct rtc_time *alm_tm)
729 {
730         unsigned char ctrl;
731
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);
741
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);
748
749         /*
750          * Account for differences between how the RTC uses the values
751          * and how they are defined in a struct rtc_time;
752          */
753         if ((alm_tm->tm_year += (epoch - 1900)) <= 69)
754                 alm_tm->tm_year += 100;
755
756         alm_tm->tm_mon--;
757 }
758
759 /*
760  * Used to disable/enable UIE and AIE interrupts.
761  */
762
763 static void mask_rtc_irq_bit(unsigned char bit)
764 {
765         unsigned char val;
766
767         spin_lock_irq(&rtc_lock);
768         val = CMOS_READ(OMAP_RTC_INTERRUPTS_REG);
769         val &=  ~bit;
770         CMOS_WRITE(val, OMAP_RTC_INTERRUPTS_REG);
771         rtc_irq_data = 0;
772         spin_unlock_irq(&rtc_lock);
773 }
774
775 static void set_rtc_irq_bit(unsigned char bit)
776 {
777         unsigned char val;
778
779         spin_lock_irq(&rtc_lock);
780         val = CMOS_READ(OMAP_RTC_INTERRUPTS_REG);
781         val |= bit;
782         CMOS_WRITE(val, OMAP_RTC_INTERRUPTS_REG);
783         rtc_irq_data = 0;
784         spin_unlock_irq(&rtc_lock);
785 }
786
787 MODULE_AUTHOR("George G. Davis");
788 MODULE_LICENSE("GPL");
789
790 module_init(rtc_init);
791 module_exit(rtc_exit);