~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/drivers/char/rtc.c

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  *      Real Time Clock interface for Linux     
  3  *
  4  *      Copyright (C) 1996 Paul Gortmaker
  5  *
  6  *      This driver allows use of the real time clock (built into
  7  *      nearly all computers) from user space. It exports the /dev/rtc
  8  *      interface supporting various ioctl() and also the
  9  *      /proc/driver/rtc pseudo-file for status information.
 10  *
 11  *      The ioctls can be used to set the interrupt behaviour and
 12  *      generation rate from the RTC via IRQ 8. Then the /dev/rtc
 13  *      interface can be used to make use of these timer interrupts,
 14  *      be they interval or alarm based.
 15  *
 16  *      The /dev/rtc interface will block on reads until an interrupt
 17  *      has been received. If a RTC interrupt has already happened,
 18  *      it will output an unsigned long and then block. The output value
 19  *      contains the interrupt status in the low byte and the number of
 20  *      interrupts since the last read in the remaining high bytes. The 
 21  *      /dev/rtc interface can also be used with the select(2) call.
 22  *
 23  *      This program is free software; you can redistribute it and/or
 24  *      modify it under the terms of the GNU General Public License
 25  *      as published by the Free Software Foundation; either version
 26  *      2 of the License, or (at your option) any later version.
 27  *
 28  *      Based on other minimal char device drivers, like Alan's
 29  *      watchdog, Ted's random, etc. etc.
 30  *
 31  *      1.07    Paul Gortmaker.
 32  *      1.08    Miquel van Smoorenburg: disallow certain things on the
 33  *              DEC Alpha as the CMOS clock is also used for other things.
 34  *      1.09    Nikita Schmidt: epoch support and some Alpha cleanup.
 35  *      1.09a   Pete Zaitcev: Sun SPARC
 36  *      1.09b   Jeff Garzik: Modularize, init cleanup
 37  *      1.09c   Jeff Garzik: SMP cleanup
 38  *      1.10    Paul Barton-Davis: add support for async I/O
 39  *      1.10a   Andrea Arcangeli: Alpha updates
 40  *      1.10b   Andrew Morton: SMP lock fix
 41  *      1.10c   Cesar Barros: SMP locking fixes and cleanup
 42  *      1.10d   Paul Gortmaker: delete paranoia check in rtc_exit
 43  */
 44 
 45 #define RTC_VERSION             "1.10d"
 46 
 47 #define RTC_IO_EXTENT   0x10    /* Only really two ports, but...        */
 48 
 49 /*
 50  *      Note that *all* calls to CMOS_READ and CMOS_WRITE are done with
 51  *      interrupts disabled. Due to the index-port/data-port (0x70/0x71)
 52  *      design of the RTC, we don't want two different things trying to
 53  *      get to it at once. (e.g. the periodic 11 min sync from time.c vs.
 54  *      this driver.)
 55  */
 56 
 57 #include <linux/config.h>
 58 #include <linux/module.h>
 59 #include <linux/kernel.h>
 60 #include <linux/types.h>
 61 #include <linux/miscdevice.h>
 62 #include <linux/ioport.h>
 63 #include <linux/fcntl.h>
 64 #include <linux/mc146818rtc.h>
 65 #include <linux/init.h>
 66 #include <linux/poll.h>
 67 #include <linux/proc_fs.h>
 68 #include <linux/spinlock.h>
 69 
 70 #include <asm/io.h>
 71 #include <asm/uaccess.h>
 72 #include <asm/system.h>
 73 
 74 #ifdef __sparc__
 75 #include <asm/ebus.h>
 76 
 77 static unsigned long rtc_port;
 78 static int rtc_irq;
 79 #endif
 80 
 81 /*
 82  *      We sponge a minor off of the misc major. No need slurping
 83  *      up another valuable major dev number for this. If you add
 84  *      an ioctl, make sure you don't conflict with SPARC's RTC
 85  *      ioctls.
 86  */
 87 
 88 static struct fasync_struct *rtc_async_queue;
 89 
 90 static DECLARE_WAIT_QUEUE_HEAD(rtc_wait);
 91 
 92 static struct timer_list rtc_irq_timer;
 93 
 94 static loff_t rtc_llseek(struct file *file, loff_t offset, int origin);
 95 
 96 static ssize_t rtc_read(struct file *file, char *buf,
 97                         size_t count, loff_t *ppos);
 98 
 99 static int rtc_ioctl(struct inode *inode, struct file *file,
100                      unsigned int cmd, unsigned long arg);
101 
102 #if RTC_IRQ
103 static unsigned int rtc_poll(struct file *file, poll_table *wait);
104 #endif
105 
106 static void get_rtc_time (struct rtc_time *rtc_tm);
107 static void get_rtc_alm_time (struct rtc_time *alm_tm);
108 #if RTC_IRQ
109 static void rtc_dropped_irq(unsigned long data);
110 
111 static void set_rtc_irq_bit(unsigned char bit);
112 static void mask_rtc_irq_bit(unsigned char bit);
113 #endif
114 
115 static inline unsigned char rtc_is_updating(void);
116 
117 static int rtc_read_proc(char *page, char **start, off_t off,
118                          int count, int *eof, void *data);
119 
120 /*
121  *      Bits in rtc_status. (6 bits of room for future expansion)
122  */
123 
124 #define RTC_IS_OPEN             0x01    /* means /dev/rtc is in use     */
125 #define RTC_TIMER_ON            0x02    /* missed irq timer active      */
126 
127 /*
128  * rtc_status is never changed by rtc_interrupt, and ioctl/open/close is
129  * protected by the big kernel lock. However, ioctl can still disable the timer
130  * in rtc_status and then with del_timer after the interrupt has read
131  * rtc_status but before mod_timer is called, which would then reenable the
132  * timer (but you would need to have an awful timing before you'd trip on it)
133  */
134 static unsigned long rtc_status = 0;    /* bitmapped status byte.       */
135 static unsigned long rtc_freq = 0;      /* Current periodic IRQ rate    */
136 static unsigned long rtc_irq_data = 0;  /* our output to the world      */
137 
138 /*
139  *      If this driver ever becomes modularised, it will be really nice
140  *      to make the epoch retain its value across module reload...
141  */
142 
143 static unsigned long epoch = 1900;      /* year corresponding to 0x00   */
144 
145 static const unsigned char days_in_mo[] = 
146 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
147 
148 #if RTC_IRQ
149 /*
150  *      A very tiny interrupt handler. It runs with SA_INTERRUPT set,
151  *      but there is possibility of conflicting with the set_rtc_mmss()
152  *      call (the rtc irq and the timer irq can easily run at the same
153  *      time in two different CPUs). So we need to serializes
154  *      accesses to the chip with the rtc_lock spinlock that each
155  *      architecture should implement in the timer code.
156  *      (See ./arch/XXXX/kernel/time.c for the set_rtc_mmss() function.)
157  */
158 
159 static void rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
160 {
161         /*
162          *      Can be an alarm interrupt, update complete interrupt,
163          *      or a periodic interrupt. We store the status in the
164          *      low byte and the number of interrupts received since
165          *      the last read in the remainder of rtc_irq_data.
166          */
167 
168         spin_lock (&rtc_lock);
169         rtc_irq_data += 0x100;
170         rtc_irq_data &= ~0xff;
171         rtc_irq_data |= (CMOS_READ(RTC_INTR_FLAGS) & 0xF0);
172 
173         if (rtc_status & RTC_TIMER_ON)
174                 mod_timer(&rtc_irq_timer, jiffies + HZ/rtc_freq + 2*HZ/100);
175 
176         spin_unlock (&rtc_lock);
177 
178         /* Now do the rest of the actions */
179         wake_up_interruptible(&rtc_wait);       
180 
181         kill_fasync (&rtc_async_queue, SIGIO, POLL_IN);
182 }
183 #endif
184 
185 /*
186  *      Now all the various file operations that we export.
187  */
188 
189 static loff_t rtc_llseek(struct file *file, loff_t offset, int origin)
190 {
191         return -ESPIPE;
192 }
193 
194 static ssize_t rtc_read(struct file *file, char *buf,
195                         size_t count, loff_t *ppos)
196 {
197 #if !RTC_IRQ
198         return -EIO;
199 #else
200         DECLARE_WAITQUEUE(wait, current);
201         unsigned long data;
202         ssize_t retval;
203         
204         if (count < sizeof(unsigned long))
205                 return -EINVAL;
206 
207         add_wait_queue(&rtc_wait, &wait);
208 
209         current->state = TASK_INTERRUPTIBLE;
210                 
211         do {
212                 /* First make it right. Then make it fast. Putting this whole
213                  * block within the parentheses of a while would be too
214                  * confusing. And no, xchg() is not the answer. */
215                 spin_lock_irq (&rtc_lock);
216                 data = rtc_irq_data;
217                 rtc_irq_data = 0;
218                 spin_unlock_irq (&rtc_lock);
219 
220                 if (data != 0)
221                         break;
222 
223                 if (file->f_flags & O_NONBLOCK) {
224                         retval = -EAGAIN;
225                         goto out;
226                 }
227                 if (signal_pending(current)) {
228                         retval = -ERESTARTSYS;
229                         goto out;
230                 }
231                 schedule();
232         } while (1);
233 
234         retval = put_user(data, (unsigned long *)buf); 
235         if (!retval)
236                 retval = sizeof(unsigned long); 
237  out:
238         current->state = TASK_RUNNING;
239         remove_wait_queue(&rtc_wait, &wait);
240 
241         return retval;
242 #endif
243 }
244 
245 static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
246                      unsigned long arg)
247 {
248         struct rtc_time wtime; 
249 
250         switch (cmd) {
251 #if RTC_IRQ
252         case RTC_AIE_OFF:       /* Mask alarm int. enab. bit    */
253         {
254                 mask_rtc_irq_bit(RTC_AIE);
255                 return 0;
256         }
257         case RTC_AIE_ON:        /* Allow alarm interrupts.      */
258         {
259                 set_rtc_irq_bit(RTC_AIE);
260                 return 0;
261         }
262         case RTC_PIE_OFF:       /* Mask periodic int. enab. bit */
263         {
264                 mask_rtc_irq_bit(RTC_PIE);
265                 if (rtc_status & RTC_TIMER_ON) {
266                         spin_lock_irq (&rtc_lock);
267                         rtc_status &= ~RTC_TIMER_ON;
268                         del_timer(&rtc_irq_timer);
269                         spin_unlock_irq (&rtc_lock);
270                 }
271                 return 0;
272         }
273         case RTC_PIE_ON:        /* Allow periodic ints          */
274         {
275 
276                 /*
277                  * We don't really want Joe User enabling more
278                  * than 64Hz of interrupts on a multi-user machine.
279                  */
280                 if ((rtc_freq > 64) && (!capable(CAP_SYS_RESOURCE)))
281                         return -EACCES;
282 
283                 if (!(rtc_status & RTC_TIMER_ON)) {
284                         spin_lock_irq (&rtc_lock);
285                         rtc_irq_timer.expires = jiffies + HZ/rtc_freq + 2*HZ/100;
286                         add_timer(&rtc_irq_timer);
287                         rtc_status |= RTC_TIMER_ON;
288                         spin_unlock_irq (&rtc_lock);
289                 }
290                 set_rtc_irq_bit(RTC_PIE);
291                 return 0;
292         }
293         case RTC_UIE_OFF:       /* Mask ints from RTC updates.  */
294         {
295                 mask_rtc_irq_bit(RTC_UIE);
296                 return 0;
297         }
298         case RTC_UIE_ON:        /* Allow ints for RTC updates.  */
299         {
300                 set_rtc_irq_bit(RTC_UIE);
301                 return 0;
302         }
303 #endif
304         case RTC_ALM_READ:      /* Read the present alarm time */
305         {
306                 /*
307                  * This returns a struct rtc_time. Reading >= 0xc0
308                  * means "don't care" or "match all". Only the tm_hour,
309                  * tm_min, and tm_sec values are filled in.
310                  */
311 
312                 get_rtc_alm_time(&wtime);
313                 break; 
314         }
315         case RTC_ALM_SET:       /* Store a time into the alarm */
316         {
317                 /*
318                  * This expects a struct rtc_time. Writing 0xff means
319                  * "don't care" or "match all". Only the tm_hour,
320                  * tm_min and tm_sec are used.
321                  */
322                 unsigned char hrs, min, sec;
323                 struct rtc_time alm_tm;
324 
325                 if (copy_from_user(&alm_tm, (struct rtc_time*)arg,
326                                    sizeof(struct rtc_time)))
327                         return -EFAULT;
328 
329                 hrs = alm_tm.tm_hour;
330                 min = alm_tm.tm_min;
331                 sec = alm_tm.tm_sec;
332 
333                 if (hrs >= 24)
334                         hrs = 0xff;
335 
336                 if (min >= 60)
337                         min = 0xff;
338 
339                 if (sec >= 60)
340                         sec = 0xff;
341 
342                 spin_lock_irq(&rtc_lock);
343                 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) ||
344                     RTC_ALWAYS_BCD)
345                 {
346                         BIN_TO_BCD(sec);
347                         BIN_TO_BCD(min);
348                         BIN_TO_BCD(hrs);
349                 }
350                 CMOS_WRITE(hrs, RTC_HOURS_ALARM);
351                 CMOS_WRITE(min, RTC_MINUTES_ALARM);
352                 CMOS_WRITE(sec, RTC_SECONDS_ALARM);
353                 spin_unlock_irq(&rtc_lock);
354 
355                 return 0;
356         }
357         case RTC_RD_TIME:       /* Read the time/date from RTC  */
358         {
359                 get_rtc_time(&wtime);
360                 break;
361         }
362         case RTC_SET_TIME:      /* Set the RTC */
363         {
364                 struct rtc_time rtc_tm;
365                 unsigned char mon, day, hrs, min, sec, leap_yr;
366                 unsigned char save_control, save_freq_select;
367                 unsigned int yrs;
368 
369                 if (!capable(CAP_SYS_TIME))
370                         return -EACCES;
371 
372                 if (copy_from_user(&rtc_tm, (struct rtc_time*)arg,
373                                    sizeof(struct rtc_time)))
374                         return -EFAULT;
375 
376                 yrs = rtc_tm.tm_year + 1900;
377                 mon = rtc_tm.tm_mon + 1;   /* tm_mon starts at zero */
378                 day = rtc_tm.tm_mday;
379                 hrs = rtc_tm.tm_hour;
380                 min = rtc_tm.tm_min;
381                 sec = rtc_tm.tm_sec;
382 
383                 if (yrs < 1970)
384                         return -EINVAL;
385 
386                 leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
387 
388                 if ((mon > 12) || (day == 0))
389                         return -EINVAL;
390 
391                 if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
392                         return -EINVAL;
393                         
394                 if ((hrs >= 24) || (min >= 60) || (sec >= 60))
395                         return -EINVAL;
396 
397                 if ((yrs -= epoch) > 255)    /* They are unsigned */
398                         return -EINVAL;
399 
400                 spin_lock_irq(&rtc_lock);
401                 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY)
402                     || RTC_ALWAYS_BCD) {
403                         if (yrs > 169) {
404                                 spin_unlock_irq(&rtc_lock);
405                                 return -EINVAL;
406                         }
407                         if (yrs >= 100)
408                                 yrs -= 100;
409 
410                         BIN_TO_BCD(sec);
411                         BIN_TO_BCD(min);
412                         BIN_TO_BCD(hrs);
413                         BIN_TO_BCD(day);
414                         BIN_TO_BCD(mon);
415                         BIN_TO_BCD(yrs);
416                 }
417 
418                 save_control = CMOS_READ(RTC_CONTROL);
419                 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
420                 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
421                 CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
422 
423                 CMOS_WRITE(yrs, RTC_YEAR);
424                 CMOS_WRITE(mon, RTC_MONTH);
425                 CMOS_WRITE(day, RTC_DAY_OF_MONTH);
426                 CMOS_WRITE(hrs, RTC_HOURS);
427                 CMOS_WRITE(min, RTC_MINUTES);
428                 CMOS_WRITE(sec, RTC_SECONDS);
429 
430                 CMOS_WRITE(save_control, RTC_CONTROL);
431                 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
432 
433                 spin_unlock_irq(&rtc_lock);
434                 return 0;
435         }
436 #if RTC_IRQ
437         case RTC_IRQP_READ:     /* Read the periodic IRQ rate.  */
438         {
439                 return put_user(rtc_freq, (unsigned long *)arg);
440         }
441         case RTC_IRQP_SET:      /* Set periodic IRQ rate.       */
442         {
443                 int tmp = 0;
444                 unsigned char val;
445 
446                 /* 
447                  * The max we can do is 8192Hz.
448                  */
449                 if ((arg < 2) || (arg > 8192))
450                         return -EINVAL;
451                 /*
452                  * We don't really want Joe User generating more
453                  * than 64Hz of interrupts on a multi-user machine.
454                  */
455                 if ((arg > 64) && (!capable(CAP_SYS_RESOURCE)))
456                         return -EACCES;
457 
458                 while (arg > (1<<tmp))
459                         tmp++;
460 
461                 /*
462                  * Check that the input was really a power of 2.
463                  */
464                 if (arg != (1<<tmp))
465                         return -EINVAL;
466 
467                 spin_lock_irq(&rtc_lock);
468                 rtc_freq = arg;
469 
470                 val = CMOS_READ(RTC_FREQ_SELECT) & 0xf0;
471                 val |= (16 - tmp);
472                 CMOS_WRITE(val, RTC_FREQ_SELECT);
473                 spin_unlock_irq(&rtc_lock);
474                 return 0;
475         }
476 #elif !defined(CONFIG_DECSTATION)
477         case RTC_EPOCH_READ:    /* Read the epoch.      */
478         {
479                 return put_user (epoch, (unsigned long *)arg);
480         }
481         case RTC_EPOCH_SET:     /* Set the epoch.       */
482         {
483                 /* 
484                  * There were no RTC clocks before 1900.
485                  */
486                 if (arg < 1900)
487                         return -EINVAL;
488 
489                 if (!capable(CAP_SYS_TIME))
490                         return -EACCES;
491 
492                 epoch = arg;
493                 return 0;
494         }
495 #endif
496         default:
497                 return -EINVAL;
498         }
499         return copy_to_user((void *)arg, &wtime, sizeof wtime) ? -EFAULT : 0;
500 }
501 
502 /*
503  *      We enforce only one user at a time here with the open/close.
504  *      Also clear the previous interrupt data on an open, and clean
505  *      up things on a close.
506  */
507 
508 /* We use rtc_lock to protect against concurrent opens. So the BKL is not
509  * needed here. Or anywhere else in this driver. */
510 static int rtc_open(struct inode *inode, struct file *file)
511 {
512         spin_lock_irq (&rtc_lock);
513 
514         if(rtc_status & RTC_IS_OPEN)
515                 goto out_busy;
516 
517         rtc_status |= RTC_IS_OPEN;
518 
519         rtc_irq_data = 0;
520         spin_unlock_irq (&rtc_lock);
521         return 0;
522 
523 out_busy:
524         spin_unlock_irq (&rtc_lock);
525         return -EBUSY;
526 }
527 
528 static int rtc_fasync (int fd, struct file *filp, int on)
529 
530 {
531         return fasync_helper (fd, filp, on, &rtc_async_queue);
532 }
533 
534 static int rtc_release(struct inode *inode, struct file *file)
535 {
536 #if RTC_IRQ
537         /*
538          * Turn off all interrupts once the device is no longer
539          * in use, and clear the data.
540          */
541 
542         unsigned char tmp;
543 
544         spin_lock_irq(&rtc_lock);
545         tmp = CMOS_READ(RTC_CONTROL);
546         tmp &=  ~RTC_PIE;
547         tmp &=  ~RTC_AIE;
548         tmp &=  ~RTC_UIE;
549         CMOS_WRITE(tmp, RTC_CONTROL);
550         CMOS_READ(RTC_INTR_FLAGS);
551 
552         if (rtc_status & RTC_TIMER_ON) {
553                 rtc_status &= ~RTC_TIMER_ON;
554                 del_timer(&rtc_irq_timer);
555         }
556         spin_unlock_irq(&rtc_lock);
557 
558         if (file->f_flags & FASYNC) {
559                 rtc_fasync (-1, file, 0);
560         }
561 #endif
562 
563         spin_lock_irq (&rtc_lock);
564         rtc_irq_data = 0;
565         spin_unlock_irq (&rtc_lock);
566 
567         /* No need for locking -- nobody else can do anything until this rmw is
568          * committed, and no timer is running. */
569         rtc_status &= ~RTC_IS_OPEN;
570         return 0;
571 }
572 
573 #if RTC_IRQ
574 /* Called without the kernel lock - fine */
575 static unsigned int rtc_poll(struct file *file, poll_table *wait)
576 {
577         unsigned long l;
578 
579         poll_wait(file, &rtc_wait, wait);
580 
581         spin_lock_irq (&rtc_lock);
582         l = rtc_irq_data;
583         spin_unlock_irq (&rtc_lock);
584 
585         if (l != 0)
586                 return POLLIN | POLLRDNORM;
587         return 0;
588 }
589 #endif
590 
591 /*
592  *      The various file operations we support.
593  */
594 
595 static struct file_operations rtc_fops = {
596         owner:          THIS_MODULE,
597         llseek:         rtc_llseek,
598         read:           rtc_read,
599 #if RTC_IRQ
600         poll:           rtc_poll,
601 #endif
602         ioctl:          rtc_ioctl,
603         open:           rtc_open,
604         release:        rtc_release,
605         fasync:         rtc_fasync,
606 };
607 
608 static struct miscdevice rtc_dev=
609 {
610         RTC_MINOR,
611         "rtc",
612         &rtc_fops
613 };
614 
615 static int __init rtc_init(void)
616 {
617 #if defined(__alpha__) || defined(__mips__)
618         unsigned int year, ctrl;
619         unsigned long uip_watchdog;
620         char *guess = NULL;
621 #endif
622 #ifdef __sparc__
623         struct linux_ebus *ebus;
624         struct linux_ebus_device *edev;
625 #endif
626 
627 #ifdef __sparc__
628         for_each_ebus(ebus) {
629                 for_each_ebusdev(edev, ebus) {
630                         if(strcmp(edev->prom_name, "rtc") == 0) {
631                                 goto found;
632                         }
633                 }
634         }
635         printk(KERN_ERR "rtc_init: no PC rtc found\n");
636         return -EIO;
637 
638 found:
639         rtc_port = edev->resource[0].start;
640         rtc_irq = edev->irqs[0];
641         /*
642          * XXX Interrupt pin #7 in Espresso is shared between RTC and
643          * PCI Slot 2 INTA# (and some INTx# in Slot 1). SA_INTERRUPT here
644          * is asking for trouble with add-on boards. Change to SA_SHIRQ.
645          */
646         if(request_irq(rtc_irq, rtc_interrupt, SA_INTERRUPT, "rtc", (void *)&rtc_port)) {
647                 /*
648                  * Standard way for sparc to print irq's is to use
649                  * __irq_itoa(). I think for EBus it's ok to use %d.
650                  */
651                 printk(KERN_ERR "rtc: cannot register IRQ %d\n", rtc_irq);
652                 return -EIO;
653         }
654 #else
655         if (check_region (RTC_PORT (0), RTC_IO_EXTENT))
656         {
657                 printk(KERN_ERR "rtc: I/O port %d is not free.\n", RTC_PORT (0));
658                 return -EIO;
659         }
660 
661 #if RTC_IRQ
662         if(request_irq(RTC_IRQ, rtc_interrupt, SA_INTERRUPT, "rtc", NULL))
663         {
664                 /* Yeah right, seeing as irq 8 doesn't even hit the bus. */
665                 printk(KERN_ERR "rtc: IRQ %d is not free.\n", RTC_IRQ);
666                 return -EIO;
667         }
668 #endif
669 
670         request_region(RTC_PORT(0), RTC_IO_EXTENT, "rtc");
671 #endif /* __sparc__ vs. others */
672 
673         misc_register(&rtc_dev);
674         create_proc_read_entry ("driver/rtc", 0, 0, rtc_read_proc, NULL);
675 
676 #if defined(__alpha__) || defined(__mips__)
677         rtc_freq = HZ;
678         
679         /* Each operating system on an Alpha uses its own epoch.
680            Let's try to guess which one we are using now. */
681         
682         uip_watchdog = jiffies;
683         if (rtc_is_updating() != 0)
684                 while (jiffies - uip_watchdog < 2*HZ/100)
685                         barrier();
686         
687         spin_lock_irq(&rtc_lock);
688         year = CMOS_READ(RTC_YEAR);
689         ctrl = CMOS_READ(RTC_CONTROL);
690         spin_unlock_irq(&rtc_lock);
691         
692         if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
693                 BCD_TO_BIN(year);       /* This should never happen... */
694         
695         if (year >= 20 && year < 48) {
696                 epoch = 1980;
697                 guess = "ARC console";
698         } else if (year >= 48 && year < 70) {
699                 epoch = 1952;
700                 guess = "Digital UNIX";
701         } else if (year >= 70 && year < 100) {
702                 epoch = 1928;
703                 guess = "Digital DECstation";
704         }
705         if (guess)
706                 printk(KERN_INFO "rtc: %s epoch (%lu) detected\n", guess, epoch);
707 #endif
708 #if RTC_IRQ
709         init_timer(&rtc_irq_timer);
710         rtc_irq_timer.function = rtc_dropped_irq;
711         spin_lock_irq(&rtc_lock);
712         /* Initialize periodic freq. to CMOS reset default, which is 1024Hz */
713         CMOS_WRITE(((CMOS_READ(RTC_FREQ_SELECT) & 0xF0) | 0x06), RTC_FREQ_SELECT);
714         spin_unlock_irq(&rtc_lock);
715         rtc_freq = 1024;
716 #endif
717 
718         printk(KERN_INFO "Real Time Clock Driver v" RTC_VERSION "\n");
719 
720         return 0;
721 }
722 
723 static void __exit rtc_exit (void)
724 {
725         remove_proc_entry ("driver/rtc", NULL);
726         misc_deregister(&rtc_dev);
727 
728 #ifdef __sparc__
729         free_irq (rtc_irq, &rtc_port);
730 #else
731         release_region (RTC_PORT (0), RTC_IO_EXTENT);
732 #if RTC_IRQ
733         free_irq (RTC_IRQ, NULL);
734 #endif
735 #endif /* __sparc__ */
736 }
737 
738 module_init(rtc_init);
739 module_exit(rtc_exit);
740 EXPORT_NO_SYMBOLS;
741 
742 #if RTC_IRQ
743 /*
744  *      At IRQ rates >= 4096Hz, an interrupt may get lost altogether.
745  *      (usually during an IDE disk interrupt, with IRQ unmasking off)
746  *      Since the interrupt handler doesn't get called, the IRQ status
747  *      byte doesn't get read, and the RTC stops generating interrupts.
748  *      A timer is set, and will call this function if/when that happens.
749  *      To get it out of this stalled state, we just read the status.
750  *      At least a jiffy of interrupts (rtc_freq/HZ) will have been lost.
751  *      (You *really* shouldn't be trying to use a non-realtime system 
752  *      for something that requires a steady > 1KHz signal anyways.)
753  */
754 
755 static void rtc_dropped_irq(unsigned long data)
756 {
757         unsigned long freq;
758 
759         spin_lock_irq (&rtc_lock);
760 
761         /* Just in case someone disabled the timer from behind our back... */
762         if (rtc_status & RTC_TIMER_ON)
763                 mod_timer(&rtc_irq_timer, jiffies + HZ/rtc_freq + 2*HZ/100);
764 
765         rtc_irq_data += ((rtc_freq/HZ)<<8);
766         rtc_irq_data &= ~0xff;
767         rtc_irq_data |= (CMOS_READ(RTC_INTR_FLAGS) & 0xF0);     /* restart */
768 
769         freq = rtc_freq;
770 
771         spin_unlock_irq(&rtc_lock);
772 
773         printk(KERN_WARNING "rtc: lost some interrupts at %ldHz.\n", freq);
774 
775         /* Now we have new data */
776         wake_up_interruptible(&rtc_wait);
777 
778         kill_fasync (&rtc_async_queue, SIGIO, POLL_IN);
779 }
780 #endif
781 
782 /*
783  *      Info exported via "/proc/driver/rtc".
784  */
785 
786 static int rtc_proc_output (char *buf)
787 {
788 #define YN(bit) ((ctrl & bit) ? "yes" : "no")
789 #define NY(bit) ((ctrl & bit) ? "no" : "yes")
790         char *p;
791         struct rtc_time tm;
792         unsigned char batt, ctrl;
793         unsigned long freq;
794 
795         spin_lock_irq(&rtc_lock);
796         batt = CMOS_READ(RTC_VALID) & RTC_VRT;
797         ctrl = CMOS_READ(RTC_CONTROL);
798         freq = rtc_freq;
799         spin_unlock_irq(&rtc_lock);
800 
801         p = buf;
802 
803         get_rtc_time(&tm);
804 
805         /*
806          * There is no way to tell if the luser has the RTC set for local
807          * time or for Universal Standard Time (GMT). Probably local though.
808          */
809         p += sprintf(p,
810                      "rtc_time\t: %02d:%02d:%02d\n"
811                      "rtc_date\t: %04d-%02d-%02d\n"
812                      "rtc_epoch\t: %04lu\n",
813                      tm.tm_hour, tm.tm_min, tm.tm_sec,
814                      tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, epoch);
815 
816         get_rtc_alm_time(&tm);
817 
818         /*
819          * We implicitly assume 24hr mode here. Alarm values >= 0xc0 will
820          * match any value for that particular field. Values that are
821          * greater than a valid time, but less than 0xc0 shouldn't appear.
822          */
823         p += sprintf(p, "alarm\t\t: ");
824         if (tm.tm_hour <= 24)
825                 p += sprintf(p, "%02d:", tm.tm_hour);
826         else
827                 p += sprintf(p, "**:");
828 
829         if (tm.tm_min <= 59)
830                 p += sprintf(p, "%02d:", tm.tm_min);
831         else
832                 p += sprintf(p, "**:");
833 
834         if (tm.tm_sec <= 59)
835                 p += sprintf(p, "%02d\n", tm.tm_sec);
836         else
837                 p += sprintf(p, "**\n");
838 
839         p += sprintf(p,
840                      "DST_enable\t: %s\n"
841                      "BCD\t\t: %s\n"
842                      "24hr\t\t: %s\n"
843                      "square_wave\t: %s\n"
844                      "alarm_IRQ\t: %s\n"
845                      "update_IRQ\t: %s\n"
846                      "periodic_IRQ\t: %s\n"
847                      "periodic_freq\t: %ld\n"
848                      "batt_status\t: %s\n",
849                      YN(RTC_DST_EN),
850                      NY(RTC_DM_BINARY),
851                      YN(RTC_24H),
852                      YN(RTC_SQWE),
853                      YN(RTC_AIE),
854                      YN(RTC_UIE),
855                      YN(RTC_PIE),
856                      freq,
857                      batt ? "okay" : "dead");
858 
859         return  p - buf;
860 #undef YN
861 #undef NY
862 }
863 
864 static int rtc_read_proc(char *page, char **start, off_t off,
865                          int count, int *eof, void *data)
866 {
867         int len = rtc_proc_output (page);
868         if (len <= off+count) *eof = 1;
869         *start = page + off;
870         len -= off;
871         if (len>count) len = count;
872         if (len<0) len = 0;
873         return len;
874 }
875 
876 /*
877  * Returns true if a clock update is in progress
878  */
879 /* FIXME shouldn't this be above rtc_init to make it fully inlined? */
880 static inline unsigned char rtc_is_updating(void)
881 {
882         unsigned char uip;
883 
884         spin_lock_irq(&rtc_lock);
885         uip = (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP);
886         spin_unlock_irq(&rtc_lock);
887         return uip;
888 }
889 
890 static void get_rtc_time(struct rtc_time *rtc_tm)
891 {
892         unsigned long uip_watchdog = jiffies;
893         unsigned char ctrl;
894 
895         /*
896          * read RTC once any update in progress is done. The update
897          * can take just over 2ms. We wait 10 to 20ms. There is no need to
898          * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
899          * If you need to know *exactly* when a second has started, enable
900          * periodic update complete interrupts, (via ioctl) and then 
901          * immediately read /dev/rtc which will block until you get the IRQ.
902          * Once the read clears, read the RTC time (again via ioctl). Easy.
903          */
904 
905         if (rtc_is_updating() != 0)
906                 while (jiffies - uip_watchdog < 2*HZ/100)
907                         barrier();
908 
909         /*
910          * Only the values that we read from the RTC are set. We leave
911          * tm_wday, tm_yday and tm_isdst untouched. Even though the
912          * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
913          * by the RTC when initially set to a non-zero value.
914          */
915         spin_lock_irq(&rtc_lock);
916         rtc_tm->tm_sec = CMOS_READ(RTC_SECONDS);
917         rtc_tm->tm_min = CMOS_READ(RTC_MINUTES);
918         rtc_tm->tm_hour = CMOS_READ(RTC_HOURS);
919         rtc_tm->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH);
920         rtc_tm->tm_mon = CMOS_READ(RTC_MONTH);
921         rtc_tm->tm_year = CMOS_READ(RTC_YEAR);
922         ctrl = CMOS_READ(RTC_CONTROL);
923         spin_unlock_irq(&rtc_lock);
924 
925         if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
926         {
927                 BCD_TO_BIN(rtc_tm->tm_sec);
928                 BCD_TO_BIN(rtc_tm->tm_min);
929                 BCD_TO_BIN(rtc_tm->tm_hour);
930                 BCD_TO_BIN(rtc_tm->tm_mday);
931                 BCD_TO_BIN(rtc_tm->tm_mon);
932                 BCD_TO_BIN(rtc_tm->tm_year);
933         }
934 
935         /*
936          * Account for differences between how the RTC uses the values
937          * and how they are defined in a struct rtc_time;
938          */
939         if ((rtc_tm->tm_year += (epoch - 1900)) <= 69)
940                 rtc_tm->tm_year += 100;
941 
942         rtc_tm->tm_mon--;
943 }
944 
945 static void get_rtc_alm_time(struct rtc_time *alm_tm)
946 {
947         unsigned char ctrl;
948 
949         /*
950          * Only the values that we read from the RTC are set. That
951          * means only tm_hour, tm_min, and tm_sec.
952          */
953         spin_lock_irq(&rtc_lock);
954         alm_tm->tm_sec = CMOS_READ(RTC_SECONDS_ALARM);
955         alm_tm->tm_min = CMOS_READ(RTC_MINUTES_ALARM);
956         alm_tm->tm_hour = CMOS_READ(RTC_HOURS_ALARM);
957         ctrl = CMOS_READ(RTC_CONTROL);
958         spin_unlock_irq(&rtc_lock);
959 
960         if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
961         {
962                 BCD_TO_BIN(alm_tm->tm_sec);
963                 BCD_TO_BIN(alm_tm->tm_min);
964                 BCD_TO_BIN(alm_tm->tm_hour);
965         }
966 }
967 
968 #if RTC_IRQ
969 /*
970  * Used to disable/enable interrupts for any one of UIE, AIE, PIE.
971  * Rumour has it that if you frob the interrupt enable/disable
972  * bits in RTC_CONTROL, you should read RTC_INTR_FLAGS, to
973  * ensure you actually start getting interrupts. Probably for
974  * compatibility with older/broken chipset RTC implementations.
975  * We also clear out any old irq data after an ioctl() that
976  * meddles with the interrupt enable/disable bits.
977  */
978 
979 static void mask_rtc_irq_bit(unsigned char bit)
980 {
981         unsigned char val;
982 
983         spin_lock_irq(&rtc_lock);
984         val = CMOS_READ(RTC_CONTROL);
985         val &=  ~bit;
986         CMOS_WRITE(val, RTC_CONTROL);
987         CMOS_READ(RTC_INTR_FLAGS);
988 
989         rtc_irq_data = 0;
990         spin_unlock_irq(&rtc_lock);
991 }
992 
993 static void set_rtc_irq_bit(unsigned char bit)
994 {
995         unsigned char val;
996 
997         spin_lock_irq(&rtc_lock);
998         val = CMOS_READ(RTC_CONTROL);
999         val |= bit;
1000         CMOS_WRITE(val, RTC_CONTROL);
1001         CMOS_READ(RTC_INTR_FLAGS);
1002 
1003         rtc_irq_data = 0;
1004         spin_unlock_irq(&rtc_lock);
1005 }
1006 #endif
1007 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.