1 /* $Id: ds1286.c,v 1.4 1999/06/17 13:29:03 ralf Exp $
3 * Real Time Clock interface for Linux
5 * Copyright (C) 1998, 1999 Ralf Baechle
7 * Based on code written by Paul Gortmaker.
9 * This driver allows use of the real time clock (built into
10 * nearly all computers) from user space. It exports the /dev/rtc
11 * interface supporting various ioctl() and also the /proc/rtc
12 * pseudo-file for status information.
14 * The ioctls can be used to set the interrupt behaviour and
15 * generation rate from the RTC via IRQ 8. Then the /dev/rtc
16 * interface can be used to make use of these timer interrupts,
17 * be they interval or alarm based.
19 * The /dev/rtc interface will block on reads until an interrupt
20 * has been received. If a RTC interrupt has already happened,
21 * it will output an unsigned long and then block. The output value
22 * contains the interrupt status in the low byte and the number of
23 * interrupts since the last read in the remaining high bytes. The
24 * /dev/rtc interface can also be used with the select(2) call.
26 * This program is free software; you can redistribute it and/or
27 * modify it under the terms of the GNU General Public License
28 * as published by the Free Software Foundation; either version
29 * 2 of the License, or (at your option) any later version.
31 #include <linux/types.h>
32 #include <linux/errno.h>
33 #include <linux/miscdevice.h>
34 #include <linux/malloc.h>
35 #include <linux/ioport.h>
36 #include <linux/fcntl.h>
37 #include <linux/init.h>
38 #include <linux/poll.h>
39 #include <linux/rtc.h>
40 #include <linux/spinlock.h>
42 #include <asm/ds1286.h>
44 #include <asm/uaccess.h>
45 #include <asm/system.h>
47 #define DS1286_VERSION "1.0"
50 * We sponge a minor off of the misc major. No need slurping
51 * up another valuable major dev number for this. If you add
52 * an ioctl, make sure you don't conflict with SPARC's RTC
56 static DECLARE_WAIT_QUEUE_HEAD(ds1286_wait
);
58 static long long ds1286_llseek(struct file
*file
, loff_t offset
, int origin
);
60 static ssize_t
ds1286_read(struct file
*file
, char *buf
,
61 size_t count
, loff_t
*ppos
);
63 static int ds1286_ioctl(struct inode
*inode
, struct file
*file
,
64 unsigned int cmd
, unsigned long arg
);
66 static unsigned int ds1286_poll(struct file
*file
, poll_table
*wait
);
68 void get_rtc_time (struct rtc_time
*rtc_tm
);
69 void get_rtc_alm_time (struct rtc_time
*alm_tm
);
71 void set_rtc_irq_bit(unsigned char bit
);
72 void clear_rtc_irq_bit(unsigned char bit
);
74 static inline unsigned char ds1286_is_updating(void);
77 static spinlock_t ds1286_lock
= SPIN_LOCK_UNLOCKED
;
81 * Bits in rtc_status. (7 bits of room for future expansion)
84 #define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
85 #define RTC_TIMER_ON 0x02 /* missed irq timer active */
87 unsigned char ds1286_status
= 0; /* bitmapped status byte. */
88 unsigned long ds1286_freq
= 0; /* Current periodic IRQ rate */
89 unsigned long ds1286_irq_data
= 0; /* our output to the world */
91 unsigned char days_in_mo
[] =
92 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
95 * A very tiny interrupt handler. It runs with SA_INTERRUPT set,
96 * so that there is no possibility of conflicting with the
97 * set_rtc_mmss() call that happens during some timer interrupts.
98 * (See ./arch/XXXX/kernel/time.c for the set_rtc_mmss() function.)
102 * Now all the various file operations that we export.
105 static long long ds1286_llseek(struct file
*file
, loff_t offset
, int origin
)
110 static ssize_t
ds1286_read(struct file
*file
, char *buf
,
111 size_t count
, loff_t
*ppos
)
113 DECLARE_WAITQUEUE(wait
, current
);
117 if (count
< sizeof(unsigned long))
120 add_wait_queue(&ds1286_wait
, &wait
);
122 current
->state
= TASK_INTERRUPTIBLE
;
124 while ((data
= xchg(&ds1286_irq_data
, 0)) == 0) {
125 if (file
->f_flags
& O_NONBLOCK
) {
129 if (signal_pending(current
)) {
130 retval
= -ERESTARTSYS
;
136 retval
= put_user(data
, (unsigned long *)buf
);
138 retval
= sizeof(unsigned long);
140 current
->state
= TASK_RUNNING
;
141 remove_wait_queue(&ds1286_wait
, &wait
);
146 static int ds1286_ioctl(struct inode
*inode
, struct file
*file
,
147 unsigned int cmd
, unsigned long arg
)
150 struct rtc_time wtime
;
153 case RTC_AIE_OFF
: /* Mask alarm int. enab. bit */
158 if (!capable(CAP_SYS_TIME
))
161 spin_lock_irqsave(&ds1286_lock
, flags
);
162 val
= CMOS_READ(RTC_CMD
);
164 CMOS_WRITE(val
, RTC_CMD
);
165 spin_unlock_irqrestore(&ds1286_lock
, flags
);
169 case RTC_AIE_ON
: /* Allow alarm interrupts. */
174 if (!capable(CAP_SYS_TIME
))
177 spin_lock_irqsave(&ds1286_lock
, flags
);
178 val
= CMOS_READ(RTC_CMD
);
180 CMOS_WRITE(val
, RTC_CMD
);
181 spin_unlock_irqrestore(&ds1286_lock
, flags
);
185 case RTC_WIE_OFF
: /* Mask watchdog int. enab. bit */
190 if (!capable(CAP_SYS_TIME
))
193 spin_lock_irqsave(&ds1286_lock
, flags
);
194 val
= CMOS_READ(RTC_CMD
);
196 CMOS_WRITE(val
, RTC_CMD
);
197 spin_unlock_irqrestore(&ds1286_lock
, flags
);
201 case RTC_WIE_ON
: /* Allow watchdog interrupts. */
206 if (!capable(CAP_SYS_TIME
))
209 spin_lock_irqsave(&ds1286_lock
, flags
);
210 val
= CMOS_READ(RTC_CMD
);
212 CMOS_WRITE(val
, RTC_CMD
);
213 spin_unlock_irqrestore(&ds1286_lock
, flags
);
217 case RTC_ALM_READ
: /* Read the present alarm time */
220 * This returns a struct rtc_time. Reading >= 0xc0
221 * means "don't care" or "match all". Only the tm_hour,
222 * tm_min, and tm_sec values are filled in.
225 get_rtc_alm_time(&wtime
);
228 case RTC_ALM_SET
: /* Store a time into the alarm */
231 * This expects a struct rtc_time. Writing 0xff means
232 * "don't care" or "match all". Only the tm_hour,
233 * tm_min and tm_sec are used.
235 unsigned char hrs
, min
, sec
;
236 struct rtc_time alm_tm
;
238 if (!capable(CAP_SYS_TIME
))
241 if (copy_from_user(&alm_tm
, (struct rtc_time
*)arg
,
242 sizeof(struct rtc_time
)))
245 hrs
= alm_tm
.tm_hour
;
258 spin_lock(&ds1286_lock
);
259 CMOS_WRITE(hrs
, RTC_HOURS_ALARM
);
260 CMOS_WRITE(min
, RTC_MINUTES_ALARM
);
261 spin_unlock(&ds1286_lock
);
265 case RTC_RD_TIME
: /* Read the time/date from RTC */
267 get_rtc_time(&wtime
);
270 case RTC_SET_TIME
: /* Set the RTC */
272 struct rtc_time rtc_tm
;
273 unsigned char mon
, day
, hrs
, min
, sec
, leap_yr
;
274 unsigned char save_control
;
275 unsigned int yrs
, flags
;
277 if (!capable(CAP_SYS_TIME
))
280 if (copy_from_user(&rtc_tm
, (struct rtc_time
*)arg
,
281 sizeof(struct rtc_time
)))
284 yrs
= rtc_tm
.tm_year
+ 1900;
285 mon
= rtc_tm
.tm_mon
+ 1; /* tm_mon starts at zero */
286 day
= rtc_tm
.tm_mday
;
287 hrs
= rtc_tm
.tm_hour
;
294 leap_yr
= ((!(yrs
% 4) && (yrs
% 100)) || !(yrs
% 400));
296 if ((mon
> 12) || (day
== 0))
299 if (day
> (days_in_mo
[mon
] + ((mon
== 2) && leap_yr
)))
302 if ((hrs
>= 24) || (min
>= 60) || (sec
>= 60))
305 if ((yrs
-= 1940) > 255) /* They are unsigned */
318 spin_lock_irqsave(&ds1286_lock
, flags
);
319 save_control
= CMOS_READ(RTC_CMD
);
320 CMOS_WRITE((save_control
|RTC_TE
), RTC_CMD
);
322 CMOS_WRITE(yrs
, RTC_YEAR
);
323 CMOS_WRITE(mon
, RTC_MONTH
);
324 CMOS_WRITE(day
, RTC_DATE
);
325 CMOS_WRITE(hrs
, RTC_HOURS
);
326 CMOS_WRITE(min
, RTC_MINUTES
);
327 CMOS_WRITE(sec
, RTC_SECONDS
);
328 CMOS_WRITE(0, RTC_HUNDREDTH_SECOND
);
330 CMOS_WRITE(save_control
, RTC_CMD
);
331 spin_unlock_irqrestore(&ds1286_lock
, flags
);
338 return copy_to_user((void *)arg
, &wtime
, sizeof wtime
) ? -EFAULT
: 0;
342 * We enforce only one user at a time here with the open/close.
343 * Also clear the previous interrupt data on an open, and clean
344 * up things on a close.
347 static int ds1286_open(struct inode
*inode
, struct file
*file
)
349 if(ds1286_status
& RTC_IS_OPEN
)
352 ds1286_status
|= RTC_IS_OPEN
;
357 static int ds1286_release(struct inode
*inode
, struct file
*file
)
359 ds1286_status
&= ~RTC_IS_OPEN
;
363 static unsigned int ds1286_poll(struct file
*file
, poll_table
*wait
)
365 poll_wait(file
, &ds1286_wait
, wait
);
366 if (ds1286_irq_data
!= 0)
367 return POLLIN
| POLLRDNORM
;
372 * The various file operations we support.
375 static struct file_operations ds1286_fops
= {
379 NULL
, /* No readdir */
388 static struct miscdevice ds1286_dev
=
395 int __init
ds1286_init(void)
397 printk(KERN_INFO
"DS1286 Real Time Clock Driver v%s\n", DS1286_VERSION
);
398 misc_register(&ds1286_dev
);
403 static char *days
[] = {
404 "***", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
408 * Info exported via "/proc/rtc".
410 int get_ds1286_status(char *buf
)
414 unsigned char hundredth
, month
, cmd
, amode
;
419 hundredth
= CMOS_READ(RTC_HUNDREDTH_SECOND
);
420 hundredth
= BCD_TO_BIN(hundredth
);
423 "rtc_time\t: %02d:%02d:%02d.%02d\n"
424 "rtc_date\t: %04d-%02d-%02d\n",
425 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
, hundredth
,
426 tm
.tm_year
+ 1900, tm
.tm_mon
+ 1, tm
.tm_mday
);
429 * We implicitly assume 24hr mode here. Alarm values >= 0xc0 will
430 * match any value for that particular field. Values that are
431 * greater than a valid time, but less than 0xc0 shouldn't appear.
433 get_rtc_alm_time(&tm
);
434 p
+= sprintf(p
, "alarm\t\t: %s ", days
[tm
.tm_wday
]);
435 if (tm
.tm_hour
<= 24)
436 p
+= sprintf(p
, "%02d:", tm
.tm_hour
);
438 p
+= sprintf(p
, "**:");
441 p
+= sprintf(p
, "%02d\n", tm
.tm_min
);
443 p
+= sprintf(p
, "**\n");
445 month
= CMOS_READ(RTC_MONTH
);
448 "square_wave\t: %s\n",
449 (month
& RTC_EOSC
) ? "disabled" : "enabled",
450 (month
& RTC_ESQW
) ? "disabled" : "enabled");
452 amode
= ((CMOS_READ(RTC_MINUTES_ALARM
) & 0x80) >> 5) |
453 ((CMOS_READ(RTC_HOURS_ALARM
) & 0x80) >> 6) |
454 ((CMOS_READ(RTC_DAY_ALARM
) & 0x80) >> 7);
455 if (amode
== 7) s
= "each minute";
456 else if (amode
== 3) s
= "minutes match";
457 else if (amode
== 1) s
= "hours and minutes match";
458 else if (amode
== 0) s
= "days, hours and minutes match";
460 p
+= sprintf(p
, "alarm_mode\t: %s\n", s
);
462 cmd
= CMOS_READ(RTC_CMD
);
464 "alarm_enable\t: %s\n"
467 "wdog_alarm_mask\t: %s\n"
468 "interrupt_mode\t: %s\n"
469 "INTB_mode\t: %s_active\n"
470 "interrupt_pins\t: %s\n",
471 (cmd
& RTC_TDF
) ? "yes" : "no",
472 (cmd
& RTC_WAF
) ? "yes" : "no",
473 (cmd
& RTC_TDM
) ? "disabled" : "enabled",
474 (cmd
& RTC_WAM
) ? "disabled" : "enabled",
475 (cmd
& RTC_PU_LVL
) ? "pulse" : "level",
476 (cmd
& RTC_IBH_LO
) ? "low" : "high",
477 (cmd
& RTC_IPSW
) ? "unswapped" : "swapped");
483 * Returns true if a clock update is in progress
485 static inline unsigned char ds1286_is_updating(void)
487 return CMOS_READ(RTC_CMD
) & RTC_TE
;
490 void get_rtc_time(struct rtc_time
*rtc_tm
)
492 unsigned long uip_watchdog
= jiffies
;
493 unsigned char save_control
;
497 * read RTC once any update in progress is done. The update
498 * can take just over 2ms. We wait 10 to 20ms. There is no need to
499 * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
500 * If you need to know *exactly* when a second has started, enable
501 * periodic update complete interrupts, (via ioctl) and then
502 * immediately read /dev/rtc which will block until you get the IRQ.
503 * Once the read clears, read the RTC time (again via ioctl). Easy.
506 if (ds1286_is_updating() != 0)
507 while (jiffies
- uip_watchdog
< 2*HZ
/100)
511 * Only the values that we read from the RTC are set. We leave
512 * tm_wday, tm_yday and tm_isdst untouched. Even though the
513 * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
514 * by the RTC when initially set to a non-zero value.
516 spin_lock_irqsave(&ds1286_lock
, flags
);
517 save_control
= CMOS_READ(RTC_CMD
);
518 CMOS_WRITE((save_control
|RTC_TE
), RTC_CMD
);
520 rtc_tm
->tm_sec
= CMOS_READ(RTC_SECONDS
);
521 rtc_tm
->tm_min
= CMOS_READ(RTC_MINUTES
);
522 rtc_tm
->tm_hour
= CMOS_READ(RTC_HOURS
) & 0x1f;
523 rtc_tm
->tm_mday
= CMOS_READ(RTC_DATE
);
524 rtc_tm
->tm_mon
= CMOS_READ(RTC_MONTH
) & 0x1f;
525 rtc_tm
->tm_year
= CMOS_READ(RTC_YEAR
);
527 CMOS_WRITE(save_control
, RTC_CMD
);
528 spin_unlock_irqrestore(&ds1286_lock
, flags
);
530 BCD_TO_BIN(rtc_tm
->tm_sec
);
531 BCD_TO_BIN(rtc_tm
->tm_min
);
532 BCD_TO_BIN(rtc_tm
->tm_hour
);
533 BCD_TO_BIN(rtc_tm
->tm_mday
);
534 BCD_TO_BIN(rtc_tm
->tm_mon
);
535 BCD_TO_BIN(rtc_tm
->tm_year
);
538 * Account for differences between how the RTC uses the values
539 * and how they are defined in a struct rtc_time;
541 if (rtc_tm
->tm_year
< 45)
542 rtc_tm
->tm_year
+= 30;
543 if ((rtc_tm
->tm_year
+= 40) < 70)
544 rtc_tm
->tm_year
+= 100;
549 void get_rtc_alm_time(struct rtc_time
*alm_tm
)
555 * Only the values that we read from the RTC are set. That
556 * means only tm_wday, tm_hour, tm_min.
558 spin_lock_irqsave(&ds1286_lock
, flags
);
559 alm_tm
->tm_min
= CMOS_READ(RTC_MINUTES_ALARM
) & 0x7f;
560 alm_tm
->tm_hour
= CMOS_READ(RTC_HOURS_ALARM
) & 0x1f;
561 alm_tm
->tm_wday
= CMOS_READ(RTC_DAY_ALARM
) & 0x07;
562 cmd
= CMOS_READ(RTC_CMD
);
563 spin_unlock_irqrestore(&ds1286_lock
, flags
);
565 BCD_TO_BIN(alm_tm
->tm_min
);
566 BCD_TO_BIN(alm_tm
->tm_hour
);