blob: 0a826d7be10ee21b85ef5178d3830aac0ef46e9a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * DS1286 Real Time Clock interface for Linux
3 *
4 * Copyright (C) 1998, 1999, 2000 Ralf Baechle
5 *
6 * Based on code written by Paul Gortmaker.
7 *
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
11 * information.
12 *
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.
16 *
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
22 * the select(2) call.
23 *
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.
28 */
29#include <linux/ds1286.h>
Arnd Bergmann7ccef462008-05-20 19:15:49 +020030#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/types.h>
32#include <linux/errno.h>
33#include <linux/miscdevice.h>
34#include <linux/slab.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>
41#include <linux/bcd.h>
42#include <linux/proc_fs.h>
Julia Lawallac0e4b72008-04-30 00:53:58 -070043#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#include <asm/uaccess.h>
46#include <asm/system.h>
47
48#define DS1286_VERSION "1.0"
49
50/*
51 * We sponge a minor off of the misc major. No need slurping
52 * up another valuable major dev number for this. If you add
53 * an ioctl, make sure you don't conflict with SPARC's RTC
54 * ioctls.
55 */
56
57static DECLARE_WAIT_QUEUE_HEAD(ds1286_wait);
58
59static ssize_t ds1286_read(struct file *file, char *buf,
60 size_t count, loff_t *ppos);
61
62static int ds1286_ioctl(struct inode *inode, struct file *file,
63 unsigned int cmd, unsigned long arg);
64
65static unsigned int ds1286_poll(struct file *file, poll_table *wait);
66
67static void ds1286_get_alm_time (struct rtc_time *alm_tm);
68static void ds1286_get_time(struct rtc_time *rtc_tm);
69static int ds1286_set_time(struct rtc_time *rtc_tm);
70
71static inline unsigned char ds1286_is_updating(void);
72
73static DEFINE_SPINLOCK(ds1286_lock);
74
75static int ds1286_read_proc(char *page, char **start, off_t off,
76 int count, int *eof, void *data);
77
78/*
79 * Bits in rtc_status. (7 bits of room for future expansion)
80 */
81
82#define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
83#define RTC_TIMER_ON 0x02 /* missed irq timer active */
84
85static unsigned char ds1286_status; /* bitmapped status byte. */
86
87static unsigned char days_in_mo[] = {
88 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
89};
90
91/*
92 * Now all the various file operations that we export.
93 */
94
95static ssize_t ds1286_read(struct file *file, char *buf,
96 size_t count, loff_t *ppos)
97{
98 return -EIO;
99}
100
101static int ds1286_ioctl(struct inode *inode, struct file *file,
102 unsigned int cmd, unsigned long arg)
103{
104 struct rtc_time wtime;
105
106 switch (cmd) {
107 case RTC_AIE_OFF: /* Mask alarm int. enab. bit */
108 {
Alexey Dobriyanc53421b2006-09-30 23:27:37 -0700109 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 unsigned char val;
111
112 if (!capable(CAP_SYS_TIME))
113 return -EACCES;
114
115 spin_lock_irqsave(&ds1286_lock, flags);
116 val = rtc_read(RTC_CMD);
117 val |= RTC_TDM;
118 rtc_write(val, RTC_CMD);
119 spin_unlock_irqrestore(&ds1286_lock, flags);
120
121 return 0;
122 }
123 case RTC_AIE_ON: /* Allow alarm interrupts. */
124 {
Alexey Dobriyanc53421b2006-09-30 23:27:37 -0700125 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 unsigned char val;
127
128 if (!capable(CAP_SYS_TIME))
129 return -EACCES;
130
131 spin_lock_irqsave(&ds1286_lock, flags);
132 val = rtc_read(RTC_CMD);
133 val &= ~RTC_TDM;
134 rtc_write(val, RTC_CMD);
135 spin_unlock_irqrestore(&ds1286_lock, flags);
136
137 return 0;
138 }
139 case RTC_WIE_OFF: /* Mask watchdog int. enab. bit */
140 {
Alexey Dobriyanc53421b2006-09-30 23:27:37 -0700141 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 unsigned char val;
143
144 if (!capable(CAP_SYS_TIME))
145 return -EACCES;
146
147 spin_lock_irqsave(&ds1286_lock, flags);
148 val = rtc_read(RTC_CMD);
149 val |= RTC_WAM;
150 rtc_write(val, RTC_CMD);
151 spin_unlock_irqrestore(&ds1286_lock, flags);
152
153 return 0;
154 }
155 case RTC_WIE_ON: /* Allow watchdog interrupts. */
156 {
Alexey Dobriyanc53421b2006-09-30 23:27:37 -0700157 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 unsigned char val;
159
160 if (!capable(CAP_SYS_TIME))
161 return -EACCES;
162
163 spin_lock_irqsave(&ds1286_lock, flags);
164 val = rtc_read(RTC_CMD);
165 val &= ~RTC_WAM;
166 rtc_write(val, RTC_CMD);
167 spin_unlock_irqrestore(&ds1286_lock, flags);
168
169 return 0;
170 }
171 case RTC_ALM_READ: /* Read the present alarm time */
172 {
173 /*
174 * This returns a struct rtc_time. Reading >= 0xc0
175 * means "don't care" or "match all". Only the tm_hour,
176 * tm_min, and tm_sec values are filled in.
177 */
178
179 memset(&wtime, 0, sizeof(wtime));
180 ds1286_get_alm_time(&wtime);
181 break;
182 }
183 case RTC_ALM_SET: /* Store a time into the alarm */
184 {
185 /*
186 * This expects a struct rtc_time. Writing 0xff means
187 * "don't care" or "match all". Only the tm_hour,
188 * tm_min and tm_sec are used.
189 */
190 unsigned char hrs, min, sec;
191 struct rtc_time alm_tm;
192
193 if (!capable(CAP_SYS_TIME))
194 return -EACCES;
195
196 if (copy_from_user(&alm_tm, (struct rtc_time*)arg,
197 sizeof(struct rtc_time)))
198 return -EFAULT;
199
200 hrs = alm_tm.tm_hour;
201 min = alm_tm.tm_min;
Ralf Baechleb35692e2007-03-08 00:14:25 +0000202 sec = alm_tm.tm_sec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 if (hrs >= 24)
205 hrs = 0xff;
206
207 if (min >= 60)
208 min = 0xff;
209
Ralf Baechleb35692e2007-03-08 00:14:25 +0000210 if (sec != 0)
211 return -EINVAL;
212
Adrian Bunk357c6e62008-10-18 20:28:42 -0700213 min = bin2bcd(min);
214 min = bin2bcd(hrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 spin_lock(&ds1286_lock);
217 rtc_write(hrs, RTC_HOURS_ALARM);
218 rtc_write(min, RTC_MINUTES_ALARM);
219 spin_unlock(&ds1286_lock);
220
221 return 0;
222 }
223 case RTC_RD_TIME: /* Read the time/date from RTC */
224 {
225 memset(&wtime, 0, sizeof(wtime));
226 ds1286_get_time(&wtime);
227 break;
228 }
229 case RTC_SET_TIME: /* Set the RTC */
230 {
231 struct rtc_time rtc_tm;
232
233 if (!capable(CAP_SYS_TIME))
234 return -EACCES;
235
236 if (copy_from_user(&rtc_tm, (struct rtc_time*)arg,
237 sizeof(struct rtc_time)))
238 return -EFAULT;
239
240 return ds1286_set_time(&rtc_tm);
241 }
242 default:
243 return -EINVAL;
244 }
245 return copy_to_user((void *)arg, &wtime, sizeof wtime) ? -EFAULT : 0;
246}
247
248/*
249 * We enforce only one user at a time here with the open/close.
250 * Also clear the previous interrupt data on an open, and clean
251 * up things on a close.
252 */
253
254static int ds1286_open(struct inode *inode, struct file *file)
255{
Arnd Bergmann7ccef462008-05-20 19:15:49 +0200256 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 spin_lock_irq(&ds1286_lock);
258
259 if (ds1286_status & RTC_IS_OPEN)
260 goto out_busy;
261
262 ds1286_status |= RTC_IS_OPEN;
263
264 spin_unlock_irq(&ds1286_lock);
Arnd Bergmann7ccef462008-05-20 19:15:49 +0200265 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return 0;
267
268out_busy:
269 spin_lock_irq(&ds1286_lock);
Arnd Bergmann7ccef462008-05-20 19:15:49 +0200270 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return -EBUSY;
272}
273
274static int ds1286_release(struct inode *inode, struct file *file)
275{
276 ds1286_status &= ~RTC_IS_OPEN;
277
278 return 0;
279}
280
281static unsigned int ds1286_poll(struct file *file, poll_table *wait)
282{
283 poll_wait(file, &ds1286_wait, wait);
284
285 return 0;
286}
287
288/*
289 * The various file operations we support.
290 */
291
Arjan van de Ven62322d22006-07-03 00:24:21 -0700292static const struct file_operations ds1286_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 .llseek = no_llseek,
294 .read = ds1286_read,
295 .poll = ds1286_poll,
296 .ioctl = ds1286_ioctl,
297 .open = ds1286_open,
298 .release = ds1286_release,
299};
300
301static struct miscdevice ds1286_dev=
302{
303 .minor = RTC_MINOR,
304 .name = "rtc",
305 .fops = &ds1286_fops,
306};
307
308static int __init ds1286_init(void)
309{
310 int err;
311
312 printk(KERN_INFO "DS1286 Real Time Clock Driver v%s\n", DS1286_VERSION);
313
314 err = misc_register(&ds1286_dev);
315 if (err)
316 goto out;
317
318 if (!create_proc_read_entry("driver/rtc", 0, 0, ds1286_read_proc, NULL)) {
319 err = -ENOMEM;
320
321 goto out_deregister;
322 }
323
324 return 0;
325
326out_deregister:
327 misc_deregister(&ds1286_dev);
328
329out:
330 return err;
331}
332
333static void __exit ds1286_exit(void)
334{
335 remove_proc_entry("driver/rtc", NULL);
336 misc_deregister(&ds1286_dev);
337}
338
339static char *days[] = {
340 "***", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
341};
342
343/*
344 * Info exported via "/proc/rtc".
345 */
346static int ds1286_proc_output(char *buf)
347{
348 char *p, *s;
349 struct rtc_time tm;
350 unsigned char hundredth, month, cmd, amode;
351
352 p = buf;
353
354 ds1286_get_time(&tm);
355 hundredth = rtc_read(RTC_HUNDREDTH_SECOND);
Adrian Bunk357c6e62008-10-18 20:28:42 -0700356 hundredth = bcd2bin(hundredth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 p += sprintf(p,
359 "rtc_time\t: %02d:%02d:%02d.%02d\n"
360 "rtc_date\t: %04d-%02d-%02d\n",
361 tm.tm_hour, tm.tm_min, tm.tm_sec, hundredth,
362 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
363
364 /*
365 * We implicitly assume 24hr mode here. Alarm values >= 0xc0 will
366 * match any value for that particular field. Values that are
367 * greater than a valid time, but less than 0xc0 shouldn't appear.
368 */
369 ds1286_get_alm_time(&tm);
370 p += sprintf(p, "alarm\t\t: %s ", days[tm.tm_wday]);
371 if (tm.tm_hour <= 24)
372 p += sprintf(p, "%02d:", tm.tm_hour);
373 else
374 p += sprintf(p, "**:");
375
376 if (tm.tm_min <= 59)
377 p += sprintf(p, "%02d\n", tm.tm_min);
378 else
379 p += sprintf(p, "**\n");
380
381 month = rtc_read(RTC_MONTH);
382 p += sprintf(p,
383 "oscillator\t: %s\n"
384 "square_wave\t: %s\n",
385 (month & RTC_EOSC) ? "disabled" : "enabled",
386 (month & RTC_ESQW) ? "disabled" : "enabled");
387
388 amode = ((rtc_read(RTC_MINUTES_ALARM) & 0x80) >> 5) |
389 ((rtc_read(RTC_HOURS_ALARM) & 0x80) >> 6) |
390 ((rtc_read(RTC_DAY_ALARM) & 0x80) >> 7);
391 if (amode == 7) s = "each minute";
392 else if (amode == 3) s = "minutes match";
393 else if (amode == 1) s = "hours and minutes match";
394 else if (amode == 0) s = "days, hours and minutes match";
395 else s = "invalid";
396 p += sprintf(p, "alarm_mode\t: %s\n", s);
397
398 cmd = rtc_read(RTC_CMD);
399 p += sprintf(p,
400 "alarm_enable\t: %s\n"
401 "wdog_alarm\t: %s\n"
402 "alarm_mask\t: %s\n"
403 "wdog_alarm_mask\t: %s\n"
404 "interrupt_mode\t: %s\n"
405 "INTB_mode\t: %s_active\n"
406 "interrupt_pins\t: %s\n",
407 (cmd & RTC_TDF) ? "yes" : "no",
408 (cmd & RTC_WAF) ? "yes" : "no",
409 (cmd & RTC_TDM) ? "disabled" : "enabled",
410 (cmd & RTC_WAM) ? "disabled" : "enabled",
411 (cmd & RTC_PU_LVL) ? "pulse" : "level",
412 (cmd & RTC_IBH_LO) ? "low" : "high",
413 (cmd & RTC_IPSW) ? "unswapped" : "swapped");
414
415 return p - buf;
416}
417
418static int ds1286_read_proc(char *page, char **start, off_t off,
419 int count, int *eof, void *data)
420{
421 int len = ds1286_proc_output (page);
422 if (len <= off+count) *eof = 1;
423 *start = page + off;
424 len -= off;
425 if (len>count)
426 len = count;
427 if (len<0)
428 len = 0;
429
430 return len;
431}
432
433/*
434 * Returns true if a clock update is in progress
435 */
436static inline unsigned char ds1286_is_updating(void)
437{
438 return rtc_read(RTC_CMD) & RTC_TE;
439}
440
441
442static void ds1286_get_time(struct rtc_time *rtc_tm)
443{
444 unsigned char save_control;
Alexey Dobriyanc53421b2006-09-30 23:27:37 -0700445 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 /*
448 * read RTC once any update in progress is done. The update
449 * can take just over 2ms. We wait 10 to 20ms. There is no need to
450 * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
451 * If you need to know *exactly* when a second has started, enable
452 * periodic update complete interrupts, (via ioctl) and then
453 * immediately read /dev/rtc which will block until you get the IRQ.
454 * Once the read clears, read the RTC time (again via ioctl). Easy.
455 */
456
457 if (ds1286_is_updating() != 0)
Jiri Slabyd05abab2008-10-15 22:04:17 -0700458 msleep(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 /*
461 * Only the values that we read from the RTC are set. We leave
462 * tm_wday, tm_yday and tm_isdst untouched. Even though the
463 * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
464 * by the RTC when initially set to a non-zero value.
465 */
466 spin_lock_irqsave(&ds1286_lock, flags);
467 save_control = rtc_read(RTC_CMD);
468 rtc_write((save_control|RTC_TE), RTC_CMD);
469
470 rtc_tm->tm_sec = rtc_read(RTC_SECONDS);
471 rtc_tm->tm_min = rtc_read(RTC_MINUTES);
472 rtc_tm->tm_hour = rtc_read(RTC_HOURS) & 0x3f;
473 rtc_tm->tm_mday = rtc_read(RTC_DATE);
474 rtc_tm->tm_mon = rtc_read(RTC_MONTH) & 0x1f;
475 rtc_tm->tm_year = rtc_read(RTC_YEAR);
476
477 rtc_write(save_control, RTC_CMD);
478 spin_unlock_irqrestore(&ds1286_lock, flags);
479
Adrian Bunk357c6e62008-10-18 20:28:42 -0700480 rtc_tm->tm_sec = bcd2bin(rtc_tm->tm_sec);
481 rtc_tm->tm_min = bcd2bin(rtc_tm->tm_min);
482 rtc_tm->tm_hour = bcd2bin(rtc_tm->tm_hour);
483 rtc_tm->tm_mday = bcd2bin(rtc_tm->tm_mday);
484 rtc_tm->tm_mon = bcd2bin(rtc_tm->tm_mon);
485 rtc_tm->tm_year = bcd2bin(rtc_tm->tm_year);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 /*
488 * Account for differences between how the RTC uses the values
489 * and how they are defined in a struct rtc_time;
490 */
491 if (rtc_tm->tm_year < 45)
492 rtc_tm->tm_year += 30;
493 if ((rtc_tm->tm_year += 40) < 70)
494 rtc_tm->tm_year += 100;
495
496 rtc_tm->tm_mon--;
497}
498
499static int ds1286_set_time(struct rtc_time *rtc_tm)
500{
501 unsigned char mon, day, hrs, min, sec, leap_yr;
502 unsigned char save_control;
Alexey Dobriyanc53421b2006-09-30 23:27:37 -0700503 unsigned int yrs;
504 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506
507 yrs = rtc_tm->tm_year + 1900;
508 mon = rtc_tm->tm_mon + 1; /* tm_mon starts at zero */
509 day = rtc_tm->tm_mday;
510 hrs = rtc_tm->tm_hour;
511 min = rtc_tm->tm_min;
512 sec = rtc_tm->tm_sec;
513
514 if (yrs < 1970)
515 return -EINVAL;
516
517 leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
518
519 if ((mon > 12) || (day == 0))
520 return -EINVAL;
521
522 if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
523 return -EINVAL;
524
525 if ((hrs >= 24) || (min >= 60) || (sec >= 60))
526 return -EINVAL;
527
528 if ((yrs -= 1940) > 255) /* They are unsigned */
529 return -EINVAL;
530
531 if (yrs >= 100)
532 yrs -= 100;
533
Adrian Bunk357c6e62008-10-18 20:28:42 -0700534 sec = bin2bcd(sec);
535 min = bin2bcd(min);
536 hrs = bin2bcd(hrs);
537 day = bin2bcd(day);
538 mon = bin2bcd(mon);
539 yrs = bin2bcd(yrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 spin_lock_irqsave(&ds1286_lock, flags);
542 save_control = rtc_read(RTC_CMD);
543 rtc_write((save_control|RTC_TE), RTC_CMD);
544
545 rtc_write(yrs, RTC_YEAR);
546 rtc_write(mon, RTC_MONTH);
547 rtc_write(day, RTC_DATE);
548 rtc_write(hrs, RTC_HOURS);
549 rtc_write(min, RTC_MINUTES);
550 rtc_write(sec, RTC_SECONDS);
551 rtc_write(0, RTC_HUNDREDTH_SECOND);
552
553 rtc_write(save_control, RTC_CMD);
554 spin_unlock_irqrestore(&ds1286_lock, flags);
555
556 return 0;
557}
558
559static void ds1286_get_alm_time(struct rtc_time *alm_tm)
560{
561 unsigned char cmd;
Alexey Dobriyanc53421b2006-09-30 23:27:37 -0700562 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 /*
565 * Only the values that we read from the RTC are set. That
566 * means only tm_wday, tm_hour, tm_min.
567 */
568 spin_lock_irqsave(&ds1286_lock, flags);
569 alm_tm->tm_min = rtc_read(RTC_MINUTES_ALARM) & 0x7f;
570 alm_tm->tm_hour = rtc_read(RTC_HOURS_ALARM) & 0x1f;
571 alm_tm->tm_wday = rtc_read(RTC_DAY_ALARM) & 0x07;
572 cmd = rtc_read(RTC_CMD);
573 spin_unlock_irqrestore(&ds1286_lock, flags);
574
Adrian Bunk357c6e62008-10-18 20:28:42 -0700575 alm_tm->tm_min = bcd2bin(alm_tm->tm_min);
576 alm_tm->tm_hour = bcd2bin(alm_tm->tm_hour);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 alm_tm->tm_sec = 0;
578}
579
580module_init(ds1286_init);
581module_exit(ds1286_exit);
582
583MODULE_AUTHOR("Ralf Baechle");
584MODULE_LICENSE("GPL");
585MODULE_ALIAS_MISCDEV(RTC_MINOR);