2 * DS1286 Real Time Clock interface for Linux
4 * Copyright (C) 1998, 1999, 2000 Ralf Baechle
6 * Based on code written by Paul Gortmaker.
8 * This driver allows use of the real time clock (built into nearly all
9 * computers) from user space. It exports the /dev/rtc interface supporting
10 * various ioctl() and also the /proc/rtc pseudo-file for status
13 * The ioctls can be used to set the interrupt behaviour and generation rate
14 * from the RTC via IRQ 8. Then the /dev/rtc interface can be used to make
15 * use of these timer interrupts, be they interval or alarm based.
17 * The /dev/rtc interface will block on reads until an interrupt has been
18 * received. If a RTC interrupt has already happened, it will output an
19 * unsigned long and then block. The output value contains the interrupt
20 * status in the low byte and the number of interrupts since the last read
21 * in the remaining high bytes. The /dev/rtc interface can also be used with
24 * This program is free software; you can redistribute it and/or modify it
25 * under the terms of the GNU General Public License as published by the
26 * Free Software Foundation; either version 2 of the License, or (at your
27 * option) any later version.
29 #include <linux/ds1286.h>
30 #include <linux/types.h>
31 #include <linux/errno.h>
32 #include <linux/miscdevice.h>
33 #include <linux/slab.h>
34 #include <linux/ioport.h>
35 #include <linux/fcntl.h>
36 #include <linux/init.h>
37 #include <linux/poll.h>
38 #include <linux/rtc.h>
39 #include <linux/spinlock.h>
40 #include <linux/bcd.h>
41 #include <linux/proc_fs.h>
43 #include <asm/uaccess.h>
44 #include <asm/system.h>
46 #define DS1286_VERSION "1.0"
49 * We sponge a minor off of the misc major. No need slurping
50 * up another valuable major dev number for this. If you add
51 * an ioctl, make sure you don't conflict with SPARC's RTC
55 static DECLARE_WAIT_QUEUE_HEAD(ds1286_wait
);
57 static ssize_t
ds1286_read(struct file
*file
, char *buf
,
58 size_t count
, loff_t
*ppos
);
60 static int ds1286_ioctl(struct inode
*inode
, struct file
*file
,
61 unsigned int cmd
, unsigned long arg
);
63 static unsigned int ds1286_poll(struct file
*file
, poll_table
*wait
);
65 static void ds1286_get_alm_time (struct rtc_time
*alm_tm
);
66 static void ds1286_get_time(struct rtc_time
*rtc_tm
);
67 static int ds1286_set_time(struct rtc_time
*rtc_tm
);
69 static inline unsigned char ds1286_is_updating(void);
71 static DEFINE_SPINLOCK(ds1286_lock
);
73 static int ds1286_read_proc(char *page
, char **start
, off_t off
,
74 int count
, int *eof
, void *data
);
77 * Bits in rtc_status. (7 bits of room for future expansion)
80 #define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
81 #define RTC_TIMER_ON 0x02 /* missed irq timer active */
83 static unsigned char ds1286_status
; /* bitmapped status byte. */
85 static unsigned char days_in_mo
[] = {
86 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
90 * Now all the various file operations that we export.
93 static ssize_t
ds1286_read(struct file
*file
, char *buf
,
94 size_t count
, loff_t
*ppos
)
99 static int ds1286_ioctl(struct inode
*inode
, struct file
*file
,
100 unsigned int cmd
, unsigned long arg
)
102 struct rtc_time wtime
;
105 case RTC_AIE_OFF
: /* Mask alarm int. enab. bit */
110 if (!capable(CAP_SYS_TIME
))
113 spin_lock_irqsave(&ds1286_lock
, flags
);
114 val
= rtc_read(RTC_CMD
);
116 rtc_write(val
, RTC_CMD
);
117 spin_unlock_irqrestore(&ds1286_lock
, flags
);
121 case RTC_AIE_ON
: /* Allow alarm interrupts. */
126 if (!capable(CAP_SYS_TIME
))
129 spin_lock_irqsave(&ds1286_lock
, flags
);
130 val
= rtc_read(RTC_CMD
);
132 rtc_write(val
, RTC_CMD
);
133 spin_unlock_irqrestore(&ds1286_lock
, flags
);
137 case RTC_WIE_OFF
: /* Mask watchdog int. enab. bit */
142 if (!capable(CAP_SYS_TIME
))
145 spin_lock_irqsave(&ds1286_lock
, flags
);
146 val
= rtc_read(RTC_CMD
);
148 rtc_write(val
, RTC_CMD
);
149 spin_unlock_irqrestore(&ds1286_lock
, flags
);
153 case RTC_WIE_ON
: /* Allow watchdog interrupts. */
158 if (!capable(CAP_SYS_TIME
))
161 spin_lock_irqsave(&ds1286_lock
, flags
);
162 val
= rtc_read(RTC_CMD
);
164 rtc_write(val
, RTC_CMD
);
165 spin_unlock_irqrestore(&ds1286_lock
, flags
);
169 case RTC_ALM_READ
: /* Read the present alarm time */
172 * This returns a struct rtc_time. Reading >= 0xc0
173 * means "don't care" or "match all". Only the tm_hour,
174 * tm_min, and tm_sec values are filled in.
177 memset(&wtime
, 0, sizeof(wtime
));
178 ds1286_get_alm_time(&wtime
);
181 case RTC_ALM_SET
: /* Store a time into the alarm */
184 * This expects a struct rtc_time. Writing 0xff means
185 * "don't care" or "match all". Only the tm_hour,
186 * tm_min and tm_sec are used.
188 unsigned char hrs
, min
, sec
;
189 struct rtc_time alm_tm
;
191 if (!capable(CAP_SYS_TIME
))
194 if (copy_from_user(&alm_tm
, (struct rtc_time
*)arg
,
195 sizeof(struct rtc_time
)))
198 hrs
= alm_tm
.tm_hour
;
211 spin_lock(&ds1286_lock
);
212 rtc_write(hrs
, RTC_HOURS_ALARM
);
213 rtc_write(min
, RTC_MINUTES_ALARM
);
214 spin_unlock(&ds1286_lock
);
218 case RTC_RD_TIME
: /* Read the time/date from RTC */
220 memset(&wtime
, 0, sizeof(wtime
));
221 ds1286_get_time(&wtime
);
224 case RTC_SET_TIME
: /* Set the RTC */
226 struct rtc_time rtc_tm
;
228 if (!capable(CAP_SYS_TIME
))
231 if (copy_from_user(&rtc_tm
, (struct rtc_time
*)arg
,
232 sizeof(struct rtc_time
)))
235 return ds1286_set_time(&rtc_tm
);
240 return copy_to_user((void *)arg
, &wtime
, sizeof wtime
) ? -EFAULT
: 0;
244 * We enforce only one user at a time here with the open/close.
245 * Also clear the previous interrupt data on an open, and clean
246 * up things on a close.
249 static int ds1286_open(struct inode
*inode
, struct file
*file
)
251 spin_lock_irq(&ds1286_lock
);
253 if (ds1286_status
& RTC_IS_OPEN
)
256 ds1286_status
|= RTC_IS_OPEN
;
258 spin_unlock_irq(&ds1286_lock
);
262 spin_lock_irq(&ds1286_lock
);
266 static int ds1286_release(struct inode
*inode
, struct file
*file
)
268 ds1286_status
&= ~RTC_IS_OPEN
;
273 static unsigned int ds1286_poll(struct file
*file
, poll_table
*wait
)
275 poll_wait(file
, &ds1286_wait
, wait
);
281 * The various file operations we support.
284 static const struct file_operations ds1286_fops
= {
288 .ioctl
= ds1286_ioctl
,
290 .release
= ds1286_release
,
293 static struct miscdevice ds1286_dev
=
297 .fops
= &ds1286_fops
,
300 static int __init
ds1286_init(void)
304 printk(KERN_INFO
"DS1286 Real Time Clock Driver v%s\n", DS1286_VERSION
);
306 err
= misc_register(&ds1286_dev
);
310 if (!create_proc_read_entry("driver/rtc", 0, 0, ds1286_read_proc
, NULL
)) {
319 misc_deregister(&ds1286_dev
);
325 static void __exit
ds1286_exit(void)
327 remove_proc_entry("driver/rtc", NULL
);
328 misc_deregister(&ds1286_dev
);
331 static char *days
[] = {
332 "***", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
336 * Info exported via "/proc/rtc".
338 static int ds1286_proc_output(char *buf
)
342 unsigned char hundredth
, month
, cmd
, amode
;
346 ds1286_get_time(&tm
);
347 hundredth
= rtc_read(RTC_HUNDREDTH_SECOND
);
348 BCD_TO_BIN(hundredth
);
351 "rtc_time\t: %02d:%02d:%02d.%02d\n"
352 "rtc_date\t: %04d-%02d-%02d\n",
353 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
, hundredth
,
354 tm
.tm_year
+ 1900, tm
.tm_mon
+ 1, tm
.tm_mday
);
357 * We implicitly assume 24hr mode here. Alarm values >= 0xc0 will
358 * match any value for that particular field. Values that are
359 * greater than a valid time, but less than 0xc0 shouldn't appear.
361 ds1286_get_alm_time(&tm
);
362 p
+= sprintf(p
, "alarm\t\t: %s ", days
[tm
.tm_wday
]);
363 if (tm
.tm_hour
<= 24)
364 p
+= sprintf(p
, "%02d:", tm
.tm_hour
);
366 p
+= sprintf(p
, "**:");
369 p
+= sprintf(p
, "%02d\n", tm
.tm_min
);
371 p
+= sprintf(p
, "**\n");
373 month
= rtc_read(RTC_MONTH
);
376 "square_wave\t: %s\n",
377 (month
& RTC_EOSC
) ? "disabled" : "enabled",
378 (month
& RTC_ESQW
) ? "disabled" : "enabled");
380 amode
= ((rtc_read(RTC_MINUTES_ALARM
) & 0x80) >> 5) |
381 ((rtc_read(RTC_HOURS_ALARM
) & 0x80) >> 6) |
382 ((rtc_read(RTC_DAY_ALARM
) & 0x80) >> 7);
383 if (amode
== 7) s
= "each minute";
384 else if (amode
== 3) s
= "minutes match";
385 else if (amode
== 1) s
= "hours and minutes match";
386 else if (amode
== 0) s
= "days, hours and minutes match";
388 p
+= sprintf(p
, "alarm_mode\t: %s\n", s
);
390 cmd
= rtc_read(RTC_CMD
);
392 "alarm_enable\t: %s\n"
395 "wdog_alarm_mask\t: %s\n"
396 "interrupt_mode\t: %s\n"
397 "INTB_mode\t: %s_active\n"
398 "interrupt_pins\t: %s\n",
399 (cmd
& RTC_TDF
) ? "yes" : "no",
400 (cmd
& RTC_WAF
) ? "yes" : "no",
401 (cmd
& RTC_TDM
) ? "disabled" : "enabled",
402 (cmd
& RTC_WAM
) ? "disabled" : "enabled",
403 (cmd
& RTC_PU_LVL
) ? "pulse" : "level",
404 (cmd
& RTC_IBH_LO
) ? "low" : "high",
405 (cmd
& RTC_IPSW
) ? "unswapped" : "swapped");
410 static int ds1286_read_proc(char *page
, char **start
, off_t off
,
411 int count
, int *eof
, void *data
)
413 int len
= ds1286_proc_output (page
);
414 if (len
<= off
+count
) *eof
= 1;
426 * Returns true if a clock update is in progress
428 static inline unsigned char ds1286_is_updating(void)
430 return rtc_read(RTC_CMD
) & RTC_TE
;
434 static void ds1286_get_time(struct rtc_time
*rtc_tm
)
436 unsigned char save_control
;
438 unsigned long uip_watchdog
= jiffies
;
441 * read RTC once any update in progress is done. The update
442 * can take just over 2ms. We wait 10 to 20ms. There is no need to
443 * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
444 * If you need to know *exactly* when a second has started, enable
445 * periodic update complete interrupts, (via ioctl) and then
446 * immediately read /dev/rtc which will block until you get the IRQ.
447 * Once the read clears, read the RTC time (again via ioctl). Easy.
450 if (ds1286_is_updating() != 0)
451 while (jiffies
- uip_watchdog
< 2*HZ
/100)
455 * Only the values that we read from the RTC are set. We leave
456 * tm_wday, tm_yday and tm_isdst untouched. Even though the
457 * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
458 * by the RTC when initially set to a non-zero value.
460 spin_lock_irqsave(&ds1286_lock
, flags
);
461 save_control
= rtc_read(RTC_CMD
);
462 rtc_write((save_control
|RTC_TE
), RTC_CMD
);
464 rtc_tm
->tm_sec
= rtc_read(RTC_SECONDS
);
465 rtc_tm
->tm_min
= rtc_read(RTC_MINUTES
);
466 rtc_tm
->tm_hour
= rtc_read(RTC_HOURS
) & 0x3f;
467 rtc_tm
->tm_mday
= rtc_read(RTC_DATE
);
468 rtc_tm
->tm_mon
= rtc_read(RTC_MONTH
) & 0x1f;
469 rtc_tm
->tm_year
= rtc_read(RTC_YEAR
);
471 rtc_write(save_control
, RTC_CMD
);
472 spin_unlock_irqrestore(&ds1286_lock
, flags
);
474 BCD_TO_BIN(rtc_tm
->tm_sec
);
475 BCD_TO_BIN(rtc_tm
->tm_min
);
476 BCD_TO_BIN(rtc_tm
->tm_hour
);
477 BCD_TO_BIN(rtc_tm
->tm_mday
);
478 BCD_TO_BIN(rtc_tm
->tm_mon
);
479 BCD_TO_BIN(rtc_tm
->tm_year
);
482 * Account for differences between how the RTC uses the values
483 * and how they are defined in a struct rtc_time;
485 if (rtc_tm
->tm_year
< 45)
486 rtc_tm
->tm_year
+= 30;
487 if ((rtc_tm
->tm_year
+= 40) < 70)
488 rtc_tm
->tm_year
+= 100;
493 static int ds1286_set_time(struct rtc_time
*rtc_tm
)
495 unsigned char mon
, day
, hrs
, min
, sec
, leap_yr
;
496 unsigned char save_control
;
501 yrs
= rtc_tm
->tm_year
+ 1900;
502 mon
= rtc_tm
->tm_mon
+ 1; /* tm_mon starts at zero */
503 day
= rtc_tm
->tm_mday
;
504 hrs
= rtc_tm
->tm_hour
;
505 min
= rtc_tm
->tm_min
;
506 sec
= rtc_tm
->tm_sec
;
511 leap_yr
= ((!(yrs
% 4) && (yrs
% 100)) || !(yrs
% 400));
513 if ((mon
> 12) || (day
== 0))
516 if (day
> (days_in_mo
[mon
] + ((mon
== 2) && leap_yr
)))
519 if ((hrs
>= 24) || (min
>= 60) || (sec
>= 60))
522 if ((yrs
-= 1940) > 255) /* They are unsigned */
535 spin_lock_irqsave(&ds1286_lock
, flags
);
536 save_control
= rtc_read(RTC_CMD
);
537 rtc_write((save_control
|RTC_TE
), RTC_CMD
);
539 rtc_write(yrs
, RTC_YEAR
);
540 rtc_write(mon
, RTC_MONTH
);
541 rtc_write(day
, RTC_DATE
);
542 rtc_write(hrs
, RTC_HOURS
);
543 rtc_write(min
, RTC_MINUTES
);
544 rtc_write(sec
, RTC_SECONDS
);
545 rtc_write(0, RTC_HUNDREDTH_SECOND
);
547 rtc_write(save_control
, RTC_CMD
);
548 spin_unlock_irqrestore(&ds1286_lock
, flags
);
553 static void ds1286_get_alm_time(struct rtc_time
*alm_tm
)
559 * Only the values that we read from the RTC are set. That
560 * means only tm_wday, tm_hour, tm_min.
562 spin_lock_irqsave(&ds1286_lock
, flags
);
563 alm_tm
->tm_min
= rtc_read(RTC_MINUTES_ALARM
) & 0x7f;
564 alm_tm
->tm_hour
= rtc_read(RTC_HOURS_ALARM
) & 0x1f;
565 alm_tm
->tm_wday
= rtc_read(RTC_DAY_ALARM
) & 0x07;
566 cmd
= rtc_read(RTC_CMD
);
567 spin_unlock_irqrestore(&ds1286_lock
, flags
);
569 BCD_TO_BIN(alm_tm
->tm_min
);
570 BCD_TO_BIN(alm_tm
->tm_hour
);
574 module_init(ds1286_init
);
575 module_exit(ds1286_exit
);
577 MODULE_AUTHOR("Ralf Baechle");
578 MODULE_LICENSE("GPL");
579 MODULE_ALIAS_MISCDEV(RTC_MINOR
);