blob: 4948c8bda6b1fb7d5dc1dd734f52ebc11307aa99 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Ingo Molnar5fd1fe92008-01-30 13:31:09 +01002 * Real Time Clock interface for Linux
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
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
Ingo Molnar5fd1fe92008-01-30 13:31:09 +010020 * interrupts since the last read in the remaining high bytes. The
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 * /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
Ralf Baechle12a0a702006-10-09 23:20:47 +010038 * 1.10 Paul Barton-Davis: add support for async I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 * 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 * 1.10e Maciej W. Rozycki: Handle DECstation's year weirdness.
Ralf Baechle12a0a702006-10-09 23:20:47 +010044 * 1.11 Takashi Iwai: Kernel access functions
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 * rtc_register/rtc_unregister/rtc_control
46 * 1.11a Daniele Bellucci: Audit create_proc_read_entry in rtc_init
47 * 1.12 Venkatesh Pallipadi: Hooks for emulating rtc on HPET base-timer
48 * CONFIG_HPET_EMULATE_RTC
Maciej W. Rozycki38e0e8c2006-07-10 04:45:30 -070049 * 1.12a Maciej W. Rozycki: Handle memory-mapped chips properly.
Alan Coxb7599582006-01-11 12:17:32 -080050 * 1.12ac Alan Cox: Allow read access to the day of week register
David John048cd582008-10-23 13:55:56 +053051 * 1.12b David John: Remove calls to the BKL.
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 */
53
David John048cd582008-10-23 13:55:56 +053054#define RTC_VERSION "1.12b"
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056/*
57 * Note that *all* calls to CMOS_READ and CMOS_WRITE are done with
58 * interrupts disabled. Due to the index-port/data-port (0x70/0x71)
59 * design of the RTC, we don't want two different things trying to
Michael Witten5196d202012-01-24 22:48:04 +000060 * get to it at once. (e.g. the periodic 11 min sync from
61 * kernel/time/ntp.c vs. this driver.)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 */
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <linux/interrupt.h>
65#include <linux/module.h>
66#include <linux/kernel.h>
67#include <linux/types.h>
68#include <linux/miscdevice.h>
69#include <linux/ioport.h>
70#include <linux/fcntl.h>
71#include <linux/mc146818rtc.h>
72#include <linux/init.h>
73#include <linux/poll.h>
74#include <linux/proc_fs.h>
75#include <linux/seq_file.h>
76#include <linux/spinlock.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010077#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#include <linux/sysctl.h>
79#include <linux/wait.h>
80#include <linux/bcd.h>
Luca Falavigna47f176f2005-06-28 20:44:42 -070081#include <linux/delay.h>
Alan Cox53f1b142008-07-23 21:30:32 -070082#include <linux/uaccess.h>
Christian Dietricha28ee472011-06-04 17:36:33 +020083#include <linux/ratelimit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85#include <asm/current.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Chris Wright55f93af2007-07-21 17:10:09 +020087#ifdef CONFIG_X86
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#include <asm/hpet.h>
89#endif
90
David S. Millercdee99d2007-07-19 13:59:58 -070091#ifdef CONFIG_SPARC32
David S. Miller75081322008-08-30 00:23:51 -070092#include <linux/of.h>
93#include <linux/of_device.h>
94#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96static unsigned long rtc_port;
David S. Miller75081322008-08-30 00:23:51 -070097static int rtc_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#endif
99
David Brownell0f4d3fd2008-10-15 22:03:00 -0700100#ifdef CONFIG_HPET_EMULATE_RTC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#undef RTC_IRQ
102#endif
103
104#ifdef RTC_IRQ
105static int rtc_has_irq = 1;
106#endif
107
108#ifndef CONFIG_HPET_EMULATE_RTC
109#define is_hpet_enabled() 0
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100110#define hpet_set_alarm_time(hrs, min, sec) 0
111#define hpet_set_periodic_freq(arg) 0
112#define hpet_mask_rtc_irq_bit(arg) 0
113#define hpet_set_rtc_irq_bit(arg) 0
114#define hpet_rtc_timer_init() do { } while (0)
115#define hpet_rtc_dropped_irq() 0
David Howells32fa4582008-02-28 13:29:43 +0000116#define hpet_register_irq_handler(h) ({ 0; })
117#define hpet_unregister_irq_handler(h) ({ 0; })
Andrew Morton533ffc22006-12-22 01:06:36 -0800118#ifdef RTC_IRQ
119static irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id)
120{
121 return 0;
122}
123#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#endif
125
126/*
127 * We sponge a minor off of the misc major. No need slurping
128 * up another valuable major dev number for this. If you add
129 * an ioctl, make sure you don't conflict with SPARC's RTC
130 * ioctls.
131 */
132
133static struct fasync_struct *rtc_async_queue;
134
135static DECLARE_WAIT_QUEUE_HEAD(rtc_wait);
136
137#ifdef RTC_IRQ
Kees Cook24ed9602017-08-28 11:28:21 -0700138static void rtc_dropped_irq(struct timer_list *unused);
Jiri Slaby40565f12007-02-12 00:52:31 -0800139
Kees Cook1d27e3e2017-10-04 16:27:04 -0700140static DEFINE_TIMER(rtc_irq_timer, rtc_dropped_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141#endif
142
143static ssize_t rtc_read(struct file *file, char __user *buf,
144 size_t count, loff_t *ppos);
145
Alan Cox53f1b142008-07-23 21:30:32 -0700146static long rtc_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
Adrian Bunk9580d852008-07-25 19:46:25 -0700147static void rtc_get_rtc_time(struct rtc_time *rtc_tm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149#ifdef RTC_IRQ
Al Viroafc9a422017-07-03 06:39:46 -0400150static __poll_t rtc_poll(struct file *file, poll_table *wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151#endif
152
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100153static void get_rtc_alm_time(struct rtc_time *alm_tm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154#ifdef RTC_IRQ
Takashi Iwaic3348762005-11-07 11:14:57 +0100155static void set_rtc_irq_bit_locked(unsigned char bit);
156static void mask_rtc_irq_bit_locked(unsigned char bit);
157
158static inline void set_rtc_irq_bit(unsigned char bit)
159{
160 spin_lock_irq(&rtc_lock);
161 set_rtc_irq_bit_locked(bit);
162 spin_unlock_irq(&rtc_lock);
163}
164
165static void mask_rtc_irq_bit(unsigned char bit)
166{
167 spin_lock_irq(&rtc_lock);
168 mask_rtc_irq_bit_locked(bit);
169 spin_unlock_irq(&rtc_lock);
170}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171#endif
172
Jan Beulich9cef7792006-12-13 00:35:05 -0800173#ifdef CONFIG_PROC_FS
Christoph Hellwig3f3942a2018-05-15 15:57:23 +0200174static int rtc_proc_show(struct seq_file *seq, void *v);
Jan Beulich9cef7792006-12-13 00:35:05 -0800175#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177/*
178 * Bits in rtc_status. (6 bits of room for future expansion)
179 */
180
181#define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
182#define RTC_TIMER_ON 0x02 /* missed irq timer active */
183
184/*
185 * rtc_status is never changed by rtc_interrupt, and ioctl/open/close is
David John048cd582008-10-23 13:55:56 +0530186 * protected by the spin lock rtc_lock. However, ioctl can still disable the
187 * timer in rtc_status and then with del_timer after the interrupt has read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 * rtc_status but before mod_timer is called, which would then reenable the
189 * timer (but you would need to have an awful timing before you'd trip on it)
190 */
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100191static unsigned long rtc_status; /* bitmapped status byte. */
192static unsigned long rtc_freq; /* Current periodic IRQ rate */
193static unsigned long rtc_irq_data; /* our output to the world */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194static unsigned long rtc_max_user_freq = 64; /* > this, need CAP_SYS_RESOURCE */
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196/*
197 * If this driver ever becomes modularised, it will be really nice
198 * to make the epoch retain its value across module reload...
199 */
200
201static unsigned long epoch = 1900; /* year corresponding to 0x00 */
202
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100203static const unsigned char days_in_mo[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
205
206/*
207 * Returns true if a clock update is in progress
208 */
209static inline unsigned char rtc_is_updating(void)
210{
Peter Zijlstra0b16f212006-09-25 16:24:23 -0700211 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 unsigned char uip;
213
Peter Zijlstra0b16f212006-09-25 16:24:23 -0700214 spin_lock_irqsave(&rtc_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 uip = (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP);
Peter Zijlstra0b16f212006-09-25 16:24:23 -0700216 spin_unlock_irqrestore(&rtc_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return uip;
218}
219
220#ifdef RTC_IRQ
221/*
Michael Opdenackerd88ed622013-10-13 06:14:42 +0200222 * A very tiny interrupt handler. It runs with interrupts disabled,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 * but there is possibility of conflicting with the set_rtc_mmss()
224 * call (the rtc irq and the timer irq can easily run at the same
225 * time in two different CPUs). So we need to serialize
226 * accesses to the chip with the rtc_lock spinlock that each
227 * architecture should implement in the timer code.
228 * (See ./arch/XXXX/kernel/time.c for the set_rtc_mmss() function.)
229 */
230
Adrian Bunk9580d852008-07-25 19:46:25 -0700231static irqreturn_t rtc_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
233 /*
234 * Can be an alarm interrupt, update complete interrupt,
235 * or a periodic interrupt. We store the status in the
236 * low byte and the number of interrupts received since
237 * the last read in the remainder of rtc_irq_data.
238 */
239
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100240 spin_lock(&rtc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 rtc_irq_data += 0x100;
242 rtc_irq_data &= ~0xff;
243 if (is_hpet_enabled()) {
244 /*
245 * In this case it is HPET RTC interrupt handler
246 * calling us, with the interrupt information
247 * passed as arg1, instead of irq.
248 */
249 rtc_irq_data |= (unsigned long)irq & 0xF0;
250 } else {
251 rtc_irq_data |= (CMOS_READ(RTC_INTR_FLAGS) & 0xF0);
252 }
253
254 if (rtc_status & RTC_TIMER_ON)
255 mod_timer(&rtc_irq_timer, jiffies + HZ/rtc_freq + 2*HZ/100);
256
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100257 spin_unlock(&rtc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100259 wake_up_interruptible(&rtc_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100261 kill_fasync(&rtc_async_queue, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 return IRQ_HANDLED;
264}
265#endif
266
267/*
268 * sysctl-tuning infrastructure.
269 */
Joe Perchesa1514272013-06-13 19:37:35 -0700270static struct ctl_table rtc_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 .procname = "max-user-freq",
273 .data = &rtc_max_user_freq,
274 .maxlen = sizeof(int),
275 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800276 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800278 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279};
280
Joe Perchesa1514272013-06-13 19:37:35 -0700281static struct ctl_table rtc_root[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 .procname = "rtc",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 .mode = 0555,
285 .child = rtc_table,
286 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800287 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288};
289
Joe Perchesa1514272013-06-13 19:37:35 -0700290static struct ctl_table dev_root[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 .procname = "dev",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 .mode = 0555,
294 .child = rtc_root,
295 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800296 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297};
298
299static struct ctl_table_header *sysctl_header;
300
301static int __init init_sysctl(void)
302{
Eric W. Biederman0b4d4142007-02-14 00:34:09 -0800303 sysctl_header = register_sysctl_table(dev_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return 0;
305}
306
307static void __exit cleanup_sysctl(void)
308{
309 unregister_sysctl_table(sysctl_header);
310}
311
312/*
313 * Now all the various file operations that we export.
314 */
315
316static ssize_t rtc_read(struct file *file, char __user *buf,
317 size_t count, loff_t *ppos)
318{
319#ifndef RTC_IRQ
320 return -EIO;
321#else
322 DECLARE_WAITQUEUE(wait, current);
323 unsigned long data;
324 ssize_t retval;
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (rtc_has_irq == 0)
327 return -EIO;
328
Maciej W. Rozycki38e0e8c2006-07-10 04:45:30 -0700329 /*
330 * Historically this function used to assume that sizeof(unsigned long)
331 * is the same in userspace and kernelspace. This lead to problems
332 * for configurations with multiple ABIs such a the MIPS o32 and 64
333 * ABIs supported on the same kernel. So now we support read of both
334 * 4 and 8 bytes and assume that's the sizeof(unsigned long) in the
335 * userspace ABI.
336 */
337 if (count != sizeof(unsigned int) && count != sizeof(unsigned long))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return -EINVAL;
339
340 add_wait_queue(&rtc_wait, &wait);
341
342 do {
343 /* First make it right. Then make it fast. Putting this whole
344 * block within the parentheses of a while would be too
345 * confusing. And no, xchg() is not the answer. */
346
347 __set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100348
349 spin_lock_irq(&rtc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 data = rtc_irq_data;
351 rtc_irq_data = 0;
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100352 spin_unlock_irq(&rtc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 if (data != 0)
355 break;
356
357 if (file->f_flags & O_NONBLOCK) {
358 retval = -EAGAIN;
359 goto out;
360 }
361 if (signal_pending(current)) {
362 retval = -ERESTARTSYS;
363 goto out;
364 }
365 schedule();
366 } while (1);
367
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100368 if (count == sizeof(unsigned int)) {
369 retval = put_user(data,
370 (unsigned int __user *)buf) ?: sizeof(int);
371 } else {
372 retval = put_user(data,
373 (unsigned long __user *)buf) ?: sizeof(long);
374 }
Maciej W. Rozycki38e0e8c2006-07-10 04:45:30 -0700375 if (!retval)
376 retval = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 out:
Milind Arun Choudharycc0a8fb2007-05-08 00:30:52 -0700378 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 remove_wait_queue(&rtc_wait, &wait);
380
381 return retval;
382#endif
383}
384
385static int rtc_do_ioctl(unsigned int cmd, unsigned long arg, int kernel)
386{
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100387 struct rtc_time wtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389#ifdef RTC_IRQ
390 if (rtc_has_irq == 0) {
391 switch (cmd) {
392 case RTC_AIE_OFF:
393 case RTC_AIE_ON:
394 case RTC_PIE_OFF:
395 case RTC_PIE_ON:
396 case RTC_UIE_OFF:
397 case RTC_UIE_ON:
398 case RTC_IRQP_READ:
399 case RTC_IRQP_SET:
400 return -EINVAL;
Peter Senna Tschudinf8885c22012-09-12 19:03:18 +0200401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
403#endif
404
405 switch (cmd) {
406#ifdef RTC_IRQ
407 case RTC_AIE_OFF: /* Mask alarm int. enab. bit */
408 {
409 mask_rtc_irq_bit(RTC_AIE);
410 return 0;
411 }
412 case RTC_AIE_ON: /* Allow alarm interrupts. */
413 {
414 set_rtc_irq_bit(RTC_AIE);
415 return 0;
416 }
417 case RTC_PIE_OFF: /* Mask periodic int. enab. bit */
418 {
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100419 /* can be called from isr via rtc_control() */
420 unsigned long flags;
421
422 spin_lock_irqsave(&rtc_lock, flags);
Takashi Iwaic3348762005-11-07 11:14:57 +0100423 mask_rtc_irq_bit_locked(RTC_PIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if (rtc_status & RTC_TIMER_ON) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 rtc_status &= ~RTC_TIMER_ON;
426 del_timer(&rtc_irq_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100428 spin_unlock_irqrestore(&rtc_lock, flags);
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return 0;
431 }
432 case RTC_PIE_ON: /* Allow periodic ints */
433 {
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100434 /* can be called from isr via rtc_control() */
435 unsigned long flags;
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 /*
438 * We don't really want Joe User enabling more
439 * than 64Hz of interrupts on a multi-user machine.
440 */
441 if (!kernel && (rtc_freq > rtc_max_user_freq) &&
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100442 (!capable(CAP_SYS_RESOURCE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return -EACCES;
444
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100445 spin_lock_irqsave(&rtc_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if (!(rtc_status & RTC_TIMER_ON)) {
Jiri Slaby40565f12007-02-12 00:52:31 -0800447 mod_timer(&rtc_irq_timer, jiffies + HZ/rtc_freq +
448 2*HZ/100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 rtc_status |= RTC_TIMER_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 }
Takashi Iwaic3348762005-11-07 11:14:57 +0100451 set_rtc_irq_bit_locked(RTC_PIE);
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100452 spin_unlock_irqrestore(&rtc_lock, flags);
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return 0;
455 }
456 case RTC_UIE_OFF: /* Mask ints from RTC updates. */
457 {
458 mask_rtc_irq_bit(RTC_UIE);
459 return 0;
460 }
461 case RTC_UIE_ON: /* Allow ints for RTC updates. */
462 {
463 set_rtc_irq_bit(RTC_UIE);
464 return 0;
465 }
466#endif
467 case RTC_ALM_READ: /* Read the present alarm time */
468 {
469 /*
470 * This returns a struct rtc_time. Reading >= 0xc0
471 * means "don't care" or "match all". Only the tm_hour,
472 * tm_min, and tm_sec values are filled in.
473 */
474 memset(&wtime, 0, sizeof(struct rtc_time));
475 get_rtc_alm_time(&wtime);
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100476 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
478 case RTC_ALM_SET: /* Store a time into the alarm */
479 {
480 /*
481 * This expects a struct rtc_time. Writing 0xff means
482 * "don't care" or "match all". Only the tm_hour,
483 * tm_min and tm_sec are used.
484 */
485 unsigned char hrs, min, sec;
486 struct rtc_time alm_tm;
487
488 if (copy_from_user(&alm_tm, (struct rtc_time __user *)arg,
489 sizeof(struct rtc_time)))
490 return -EFAULT;
491
492 hrs = alm_tm.tm_hour;
493 min = alm_tm.tm_min;
494 sec = alm_tm.tm_sec;
495
496 spin_lock_irq(&rtc_lock);
497 if (hpet_set_alarm_time(hrs, min, sec)) {
498 /*
499 * Fallthru and set alarm time in CMOS too,
500 * so that we will get proper value in RTC_ALM_READ
501 */
502 }
503 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) ||
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100504 RTC_ALWAYS_BCD) {
505 if (sec < 60)
Adrian Bunk357c6e62008-10-18 20:28:42 -0700506 sec = bin2bcd(sec);
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100507 else
508 sec = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100510 if (min < 60)
Adrian Bunk357c6e62008-10-18 20:28:42 -0700511 min = bin2bcd(min);
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100512 else
513 min = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100515 if (hrs < 24)
Adrian Bunk357c6e62008-10-18 20:28:42 -0700516 hrs = bin2bcd(hrs);
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100517 else
518 hrs = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520 CMOS_WRITE(hrs, RTC_HOURS_ALARM);
521 CMOS_WRITE(min, RTC_MINUTES_ALARM);
522 CMOS_WRITE(sec, RTC_SECONDS_ALARM);
523 spin_unlock_irq(&rtc_lock);
524
525 return 0;
526 }
527 case RTC_RD_TIME: /* Read the time/date from RTC */
528 {
529 memset(&wtime, 0, sizeof(struct rtc_time));
530 rtc_get_rtc_time(&wtime);
531 break;
532 }
533 case RTC_SET_TIME: /* Set the RTC */
534 {
535 struct rtc_time rtc_tm;
536 unsigned char mon, day, hrs, min, sec, leap_yr;
537 unsigned char save_control, save_freq_select;
538 unsigned int yrs;
539#ifdef CONFIG_MACH_DECSTATION
540 unsigned int real_yrs;
541#endif
542
543 if (!capable(CAP_SYS_TIME))
544 return -EACCES;
545
546 if (copy_from_user(&rtc_tm, (struct rtc_time __user *)arg,
547 sizeof(struct rtc_time)))
548 return -EFAULT;
549
550 yrs = rtc_tm.tm_year + 1900;
551 mon = rtc_tm.tm_mon + 1; /* tm_mon starts at zero */
552 day = rtc_tm.tm_mday;
553 hrs = rtc_tm.tm_hour;
554 min = rtc_tm.tm_min;
555 sec = rtc_tm.tm_sec;
556
557 if (yrs < 1970)
558 return -EINVAL;
559
560 leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
561
562 if ((mon > 12) || (day == 0))
563 return -EINVAL;
564
565 if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
566 return -EINVAL;
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if ((hrs >= 24) || (min >= 60) || (sec >= 60))
569 return -EINVAL;
570
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100571 yrs -= epoch;
572 if (yrs > 255) /* They are unsigned */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 return -EINVAL;
574
575 spin_lock_irq(&rtc_lock);
576#ifdef CONFIG_MACH_DECSTATION
577 real_yrs = yrs;
578 yrs = 72;
579
580 /*
581 * We want to keep the year set to 73 until March
582 * for non-leap years, so that Feb, 29th is handled
583 * correctly.
584 */
585 if (!leap_yr && mon < 3) {
586 real_yrs--;
587 yrs = 73;
588 }
589#endif
590 /* These limits and adjustments are independent of
591 * whether the chip is in binary mode or not.
592 */
593 if (yrs > 169) {
594 spin_unlock_irq(&rtc_lock);
595 return -EINVAL;
596 }
597 if (yrs >= 100)
598 yrs -= 100;
599
600 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY)
601 || RTC_ALWAYS_BCD) {
Adrian Bunk357c6e62008-10-18 20:28:42 -0700602 sec = bin2bcd(sec);
603 min = bin2bcd(min);
604 hrs = bin2bcd(hrs);
605 day = bin2bcd(day);
606 mon = bin2bcd(mon);
607 yrs = bin2bcd(yrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
609
610 save_control = CMOS_READ(RTC_CONTROL);
611 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
612 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
613 CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
614
615#ifdef CONFIG_MACH_DECSTATION
616 CMOS_WRITE(real_yrs, RTC_DEC_YEAR);
617#endif
618 CMOS_WRITE(yrs, RTC_YEAR);
619 CMOS_WRITE(mon, RTC_MONTH);
620 CMOS_WRITE(day, RTC_DAY_OF_MONTH);
621 CMOS_WRITE(hrs, RTC_HOURS);
622 CMOS_WRITE(min, RTC_MINUTES);
623 CMOS_WRITE(sec, RTC_SECONDS);
624
625 CMOS_WRITE(save_control, RTC_CONTROL);
626 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
627
628 spin_unlock_irq(&rtc_lock);
629 return 0;
630 }
631#ifdef RTC_IRQ
632 case RTC_IRQP_READ: /* Read the periodic IRQ rate. */
633 {
634 return put_user(rtc_freq, (unsigned long __user *)arg);
635 }
636 case RTC_IRQP_SET: /* Set periodic IRQ rate. */
637 {
638 int tmp = 0;
639 unsigned char val;
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100640 /* can be called from isr via rtc_control() */
641 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100643 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 * The max we can do is 8192Hz.
645 */
646 if ((arg < 2) || (arg > 8192))
647 return -EINVAL;
648 /*
649 * We don't really want Joe User generating more
650 * than 64Hz of interrupts on a multi-user machine.
651 */
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100652 if (!kernel && (arg > rtc_max_user_freq) &&
653 !capable(CAP_SYS_RESOURCE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return -EACCES;
655
656 while (arg > (1<<tmp))
657 tmp++;
658
659 /*
660 * Check that the input was really a power of 2.
661 */
662 if (arg != (1<<tmp))
663 return -EINVAL;
664
Paul Gortmaker61ca9da2008-07-10 17:30:48 -0700665 rtc_freq = arg;
666
Takashi Iwaic3348762005-11-07 11:14:57 +0100667 spin_lock_irqsave(&rtc_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if (hpet_set_periodic_freq(arg)) {
Takashi Iwaic3348762005-11-07 11:14:57 +0100669 spin_unlock_irqrestore(&rtc_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return 0;
671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 val = CMOS_READ(RTC_FREQ_SELECT) & 0xf0;
674 val |= (16 - tmp);
675 CMOS_WRITE(val, RTC_FREQ_SELECT);
Takashi Iwaic3348762005-11-07 11:14:57 +0100676 spin_unlock_irqrestore(&rtc_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return 0;
678 }
679#endif
680 case RTC_EPOCH_READ: /* Read the epoch. */
681 {
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100682 return put_user(epoch, (unsigned long __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
684 case RTC_EPOCH_SET: /* Set the epoch. */
685 {
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100686 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 * There were no RTC clocks before 1900.
688 */
689 if (arg < 1900)
690 return -EINVAL;
691
692 if (!capable(CAP_SYS_TIME))
693 return -EACCES;
694
695 epoch = arg;
696 return 0;
697 }
698 default:
699 return -ENOTTY;
700 }
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100701 return copy_to_user((void __user *)arg,
702 &wtime, sizeof wtime) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
Alan Cox53f1b142008-07-23 21:30:32 -0700705static long rtc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
Alan Cox53f1b142008-07-23 21:30:32 -0700707 long ret;
Alan Cox53f1b142008-07-23 21:30:32 -0700708 ret = rtc_do_ioctl(cmd, arg, 0);
Alan Cox53f1b142008-07-23 21:30:32 -0700709 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
712/*
713 * We enforce only one user at a time here with the open/close.
714 * Also clear the previous interrupt data on an open, and clean
715 * up things on a close.
716 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717static int rtc_open(struct inode *inode, struct file *file)
718{
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100719 spin_lock_irq(&rtc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100721 if (rtc_status & RTC_IS_OPEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 goto out_busy;
723
724 rtc_status |= RTC_IS_OPEN;
725
726 rtc_irq_data = 0;
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100727 spin_unlock_irq(&rtc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 return 0;
729
730out_busy:
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100731 spin_unlock_irq(&rtc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 return -EBUSY;
733}
734
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100735static int rtc_fasync(int fd, struct file *filp, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100737 return fasync_helper(fd, filp, on, &rtc_async_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738}
739
740static int rtc_release(struct inode *inode, struct file *file)
741{
742#ifdef RTC_IRQ
743 unsigned char tmp;
744
745 if (rtc_has_irq == 0)
746 goto no_irq;
747
748 /*
749 * Turn off all interrupts once the device is no longer
750 * in use, and clear the data.
751 */
752
753 spin_lock_irq(&rtc_lock);
754 if (!hpet_mask_rtc_irq_bit(RTC_PIE | RTC_AIE | RTC_UIE)) {
755 tmp = CMOS_READ(RTC_CONTROL);
756 tmp &= ~RTC_PIE;
757 tmp &= ~RTC_AIE;
758 tmp &= ~RTC_UIE;
759 CMOS_WRITE(tmp, RTC_CONTROL);
760 CMOS_READ(RTC_INTR_FLAGS);
761 }
762 if (rtc_status & RTC_TIMER_ON) {
763 rtc_status &= ~RTC_TIMER_ON;
764 del_timer(&rtc_irq_timer);
765 }
766 spin_unlock_irq(&rtc_lock);
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768no_irq:
769#endif
770
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100771 spin_lock_irq(&rtc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 rtc_irq_data = 0;
773 rtc_status &= ~RTC_IS_OPEN;
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100774 spin_unlock_irq(&rtc_lock);
775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return 0;
777}
778
779#ifdef RTC_IRQ
Al Viroafc9a422017-07-03 06:39:46 -0400780static __poll_t rtc_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
782 unsigned long l;
783
784 if (rtc_has_irq == 0)
785 return 0;
786
787 poll_wait(file, &rtc_wait, wait);
788
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100789 spin_lock_irq(&rtc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 l = rtc_irq_data;
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100791 spin_unlock_irq(&rtc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 if (l != 0)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800794 return EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return 0;
796}
797#endif
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799/*
800 * The various file operations we support.
801 */
802
Arjan van de Ven62322d22006-07-03 00:24:21 -0700803static const struct file_operations rtc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 .owner = THIS_MODULE,
805 .llseek = no_llseek,
806 .read = rtc_read,
807#ifdef RTC_IRQ
808 .poll = rtc_poll,
809#endif
Alan Cox53f1b142008-07-23 21:30:32 -0700810 .unlocked_ioctl = rtc_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 .open = rtc_open,
812 .release = rtc_release,
813 .fasync = rtc_fasync,
814};
815
816static struct miscdevice rtc_dev = {
817 .minor = RTC_MINOR,
818 .name = "rtc",
819 .fops = &rtc_fops,
820};
821
Bjorn Helgaas9626f1f12007-11-14 16:59:57 -0800822static resource_size_t rtc_size;
823
824static struct resource * __init rtc_request_region(resource_size_t size)
825{
826 struct resource *r;
827
828 if (RTC_IOMAPPED)
829 r = request_region(RTC_PORT(0), size, "rtc");
830 else
831 r = request_mem_region(RTC_PORT(0), size, "rtc");
832
833 if (r)
834 rtc_size = size;
835
836 return r;
837}
838
Bjorn Helgaas4c06be12007-11-14 16:59:56 -0800839static void rtc_release_region(void)
840{
841 if (RTC_IOMAPPED)
Bjorn Helgaas9626f1f12007-11-14 16:59:57 -0800842 release_region(RTC_PORT(0), rtc_size);
Bjorn Helgaas4c06be12007-11-14 16:59:56 -0800843 else
Bjorn Helgaas9626f1f12007-11-14 16:59:57 -0800844 release_mem_region(RTC_PORT(0), rtc_size);
Bjorn Helgaas4c06be12007-11-14 16:59:56 -0800845}
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847static int __init rtc_init(void)
848{
Jan Beulich9cef7792006-12-13 00:35:05 -0800849#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 struct proc_dir_entry *ent;
Jan Beulich9cef7792006-12-13 00:35:05 -0800851#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852#if defined(__alpha__) || defined(__mips__)
853 unsigned int year, ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 char *guess = NULL;
855#endif
David S. Millercdee99d2007-07-19 13:59:58 -0700856#ifdef CONFIG_SPARC32
David S. Miller75081322008-08-30 00:23:51 -0700857 struct device_node *ebus_dp;
Grant Likely2dc11582010-08-06 09:25:50 -0600858 struct platform_device *op;
Jan Beulich9cef7792006-12-13 00:35:05 -0800859#else
Maciej W. Rozycki38e0e8c2006-07-10 04:45:30 -0700860 void *r;
Jan Beulich9cef7792006-12-13 00:35:05 -0800861#ifdef RTC_IRQ
862 irq_handler_t rtc_int_handler_ptr;
863#endif
Maciej W. Rozycki38e0e8c2006-07-10 04:45:30 -0700864#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
David S. Millercdee99d2007-07-19 13:59:58 -0700866#ifdef CONFIG_SPARC32
David S. Miller75081322008-08-30 00:23:51 -0700867 for_each_node_by_name(ebus_dp, "ebus") {
868 struct device_node *dp;
869 for (dp = ebus_dp; dp; dp = dp->sibling) {
870 if (!strcmp(dp->name, "rtc")) {
871 op = of_find_device_by_node(dp);
872 if (op) {
873 rtc_port = op->resource[0].start;
874 rtc_irq = op->irqs[0];
875 goto found;
876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
878 }
879 }
Jan Beulichf3e92d32006-12-13 00:35:04 -0800880 rtc_has_irq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 printk(KERN_ERR "rtc_init: no PC rtc found\n");
882 return -EIO;
883
884found:
David S. Miller75081322008-08-30 00:23:51 -0700885 if (!rtc_irq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 rtc_has_irq = 0;
887 goto no_irq;
888 }
889
890 /*
891 * XXX Interrupt pin #7 in Espresso is shared between RTC and
David S. Miller53d0fc22005-09-05 23:33:05 -0700892 * PCI Slot 2 INTA# (and some INTx# in Slot 1).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 */
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100894 if (request_irq(rtc_irq, rtc_interrupt, IRQF_SHARED, "rtc",
895 (void *)&rtc_port)) {
Jan Beulichf3e92d32006-12-13 00:35:04 -0800896 rtc_has_irq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 printk(KERN_ERR "rtc: cannot register IRQ %d\n", rtc_irq);
898 return -EIO;
899 }
900no_irq:
901#else
Bjorn Helgaas9626f1f12007-11-14 16:59:57 -0800902 r = rtc_request_region(RTC_IO_EXTENT);
903
904 /*
905 * If we've already requested a smaller range (for example, because
906 * PNPBIOS or ACPI told us how the device is configured), the request
907 * above might fail because it's too big.
908 *
909 * If so, request just the range we actually use.
910 */
911 if (!r)
912 r = rtc_request_region(RTC_IO_EXTENT_USED);
Maciej W. Rozycki38e0e8c2006-07-10 04:45:30 -0700913 if (!r) {
Jan Beulichf3e92d32006-12-13 00:35:04 -0800914#ifdef RTC_IRQ
915 rtc_has_irq = 0;
916#endif
Maciej W. Rozycki38e0e8c2006-07-10 04:45:30 -0700917 printk(KERN_ERR "rtc: I/O resource %lx is not free.\n",
918 (long)(RTC_PORT(0)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 return -EIO;
920 }
921
922#ifdef RTC_IRQ
923 if (is_hpet_enabled()) {
Bernhard Wallef8f76482008-01-30 13:33:31 +0100924 int err;
925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 rtc_int_handler_ptr = hpet_rtc_interrupt;
Bernhard Wallef8f76482008-01-30 13:33:31 +0100927 err = hpet_register_irq_handler(rtc_interrupt);
928 if (err != 0) {
929 printk(KERN_WARNING "hpet_register_irq_handler failed "
930 "in rtc_init().");
931 return err;
932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 } else {
934 rtc_int_handler_ptr = rtc_interrupt;
935 }
936
Michael Opdenackerd88ed622013-10-13 06:14:42 +0200937 if (request_irq(RTC_IRQ, rtc_int_handler_ptr, 0, "rtc", NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 /* Yeah right, seeing as irq 8 doesn't even hit the bus. */
Jan Beulichf3e92d32006-12-13 00:35:04 -0800939 rtc_has_irq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 printk(KERN_ERR "rtc: IRQ %d is not free.\n", RTC_IRQ);
Bjorn Helgaas4c06be12007-11-14 16:59:56 -0800941 rtc_release_region();
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 return -EIO;
944 }
945 hpet_rtc_timer_init();
946
947#endif
948
David S. Millercdee99d2007-07-19 13:59:58 -0700949#endif /* CONFIG_SPARC32 vs. others */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 if (misc_register(&rtc_dev)) {
952#ifdef RTC_IRQ
953 free_irq(RTC_IRQ, NULL);
Bernhard Wallef8f76482008-01-30 13:33:31 +0100954 hpet_unregister_irq_handler(rtc_interrupt);
Jan Beulichf3e92d32006-12-13 00:35:04 -0800955 rtc_has_irq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956#endif
Bjorn Helgaas4c06be12007-11-14 16:59:56 -0800957 rtc_release_region();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 return -ENODEV;
959 }
960
Jan Beulich9cef7792006-12-13 00:35:05 -0800961#ifdef CONFIG_PROC_FS
Christoph Hellwig3f3942a2018-05-15 15:57:23 +0200962 ent = proc_create_single("driver/rtc", 0, NULL, rtc_proc_show);
Denis V. Lunev1b502212008-04-29 01:02:34 -0700963 if (!ent)
Jan Beulich9cef7792006-12-13 00:35:05 -0800964 printk(KERN_WARNING "rtc: Failed to register with procfs.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967#if defined(__alpha__) || defined(__mips__)
968 rtc_freq = HZ;
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 /* Each operating system on an Alpha uses its own epoch.
971 Let's try to guess which one we are using now. */
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (rtc_is_updating() != 0)
Luca Falavigna47f176f2005-06-28 20:44:42 -0700974 msleep(20);
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 spin_lock_irq(&rtc_lock);
977 year = CMOS_READ(RTC_YEAR);
978 ctrl = CMOS_READ(RTC_CONTROL);
979 spin_unlock_irq(&rtc_lock);
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
Adrian Bunk357c6e62008-10-18 20:28:42 -0700982 year = bcd2bin(year); /* This should never happen... */
Ingo Molnar5fd1fe92008-01-30 13:31:09 +0100983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 if (year < 20) {
985 epoch = 2000;
986 guess = "SRM (post-2000)";
987 } else if (year >= 20 && year < 48) {
988 epoch = 1980;
989 guess = "ARC console";
990 } else if (year >= 48 && year < 72) {
991 epoch = 1952;
992 guess = "Digital UNIX";
993#if defined(__mips__)
994 } else if (year >= 72 && year < 74) {
995 epoch = 2000;
996 guess = "Digital DECstation";
997#else
998 } else if (year >= 70) {
999 epoch = 1900;
1000 guess = "Standard PC (1900)";
1001#endif
1002 }
1003 if (guess)
Ingo Molnar5fd1fe92008-01-30 13:31:09 +01001004 printk(KERN_INFO "rtc: %s epoch (%lu) detected\n",
1005 guess, epoch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006#endif
1007#ifdef RTC_IRQ
1008 if (rtc_has_irq == 0)
1009 goto no_irq2;
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 spin_lock_irq(&rtc_lock);
1012 rtc_freq = 1024;
1013 if (!hpet_set_periodic_freq(rtc_freq)) {
Ingo Molnar5fd1fe92008-01-30 13:31:09 +01001014 /*
1015 * Initialize periodic frequency to CMOS reset default,
1016 * which is 1024Hz
1017 */
1018 CMOS_WRITE(((CMOS_READ(RTC_FREQ_SELECT) & 0xF0) | 0x06),
1019 RTC_FREQ_SELECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 }
1021 spin_unlock_irq(&rtc_lock);
1022no_irq2:
1023#endif
1024
1025 (void) init_sysctl();
1026
1027 printk(KERN_INFO "Real Time Clock Driver v" RTC_VERSION "\n");
1028
1029 return 0;
1030}
1031
Ingo Molnar5fd1fe92008-01-30 13:31:09 +01001032static void __exit rtc_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
1034 cleanup_sysctl();
Ingo Molnar5fd1fe92008-01-30 13:31:09 +01001035 remove_proc_entry("driver/rtc", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 misc_deregister(&rtc_dev);
1037
David S. Millercdee99d2007-07-19 13:59:58 -07001038#ifdef CONFIG_SPARC32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 if (rtc_has_irq)
Ingo Molnar5fd1fe92008-01-30 13:31:09 +01001040 free_irq(rtc_irq, &rtc_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041#else
Bjorn Helgaas4c06be12007-11-14 16:59:56 -08001042 rtc_release_region();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043#ifdef RTC_IRQ
Bernhard Wallef8f76482008-01-30 13:33:31 +01001044 if (rtc_has_irq) {
Ingo Molnar5fd1fe92008-01-30 13:31:09 +01001045 free_irq(RTC_IRQ, NULL);
Bernhard Wallef8f76482008-01-30 13:33:31 +01001046 hpet_unregister_irq_handler(hpet_rtc_interrupt);
1047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048#endif
David S. Millercdee99d2007-07-19 13:59:58 -07001049#endif /* CONFIG_SPARC32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050}
1051
1052module_init(rtc_init);
1053module_exit(rtc_exit);
1054
1055#ifdef RTC_IRQ
1056/*
Ingo Molnar5fd1fe92008-01-30 13:31:09 +01001057 * At IRQ rates >= 4096Hz, an interrupt may get lost altogether.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 * (usually during an IDE disk interrupt, with IRQ unmasking off)
1059 * Since the interrupt handler doesn't get called, the IRQ status
1060 * byte doesn't get read, and the RTC stops generating interrupts.
1061 * A timer is set, and will call this function if/when that happens.
1062 * To get it out of this stalled state, we just read the status.
1063 * At least a jiffy of interrupts (rtc_freq/HZ) will have been lost.
Ingo Molnar5fd1fe92008-01-30 13:31:09 +01001064 * (You *really* shouldn't be trying to use a non-realtime system
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 * for something that requires a steady > 1KHz signal anyways.)
1066 */
1067
Kees Cook24ed9602017-08-28 11:28:21 -07001068static void rtc_dropped_irq(struct timer_list *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
1070 unsigned long freq;
1071
Ingo Molnar5fd1fe92008-01-30 13:31:09 +01001072 spin_lock_irq(&rtc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074 if (hpet_rtc_dropped_irq()) {
1075 spin_unlock_irq(&rtc_lock);
1076 return;
1077 }
1078
1079 /* Just in case someone disabled the timer from behind our back... */
1080 if (rtc_status & RTC_TIMER_ON)
1081 mod_timer(&rtc_irq_timer, jiffies + HZ/rtc_freq + 2*HZ/100);
1082
1083 rtc_irq_data += ((rtc_freq/HZ)<<8);
1084 rtc_irq_data &= ~0xff;
1085 rtc_irq_data |= (CMOS_READ(RTC_INTR_FLAGS) & 0xF0); /* restart */
1086
1087 freq = rtc_freq;
1088
1089 spin_unlock_irq(&rtc_lock);
1090
Christian Dietricha28ee472011-06-04 17:36:33 +02001091 printk_ratelimited(KERN_WARNING "rtc: lost some interrupts at %ldHz.\n",
1092 freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094 /* Now we have new data */
1095 wake_up_interruptible(&rtc_wait);
1096
Ingo Molnar5fd1fe92008-01-30 13:31:09 +01001097 kill_fasync(&rtc_async_queue, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098}
1099#endif
1100
Jan Beulich9cef7792006-12-13 00:35:05 -08001101#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102/*
1103 * Info exported via "/proc/driver/rtc".
1104 */
1105
1106static int rtc_proc_show(struct seq_file *seq, void *v)
1107{
1108#define YN(bit) ((ctrl & bit) ? "yes" : "no")
1109#define NY(bit) ((ctrl & bit) ? "no" : "yes")
1110 struct rtc_time tm;
1111 unsigned char batt, ctrl;
1112 unsigned long freq;
1113
1114 spin_lock_irq(&rtc_lock);
1115 batt = CMOS_READ(RTC_VALID) & RTC_VRT;
1116 ctrl = CMOS_READ(RTC_CONTROL);
1117 freq = rtc_freq;
1118 spin_unlock_irq(&rtc_lock);
1119
1120
1121 rtc_get_rtc_time(&tm);
1122
1123 /*
1124 * There is no way to tell if the luser has the RTC set for local
1125 * time or for Universal Standard Time (GMT). Probably local though.
1126 */
1127 seq_printf(seq,
1128 "rtc_time\t: %02d:%02d:%02d\n"
1129 "rtc_date\t: %04d-%02d-%02d\n"
1130 "rtc_epoch\t: %04lu\n",
1131 tm.tm_hour, tm.tm_min, tm.tm_sec,
1132 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, epoch);
1133
1134 get_rtc_alm_time(&tm);
1135
1136 /*
1137 * We implicitly assume 24hr mode here. Alarm values >= 0xc0 will
1138 * match any value for that particular field. Values that are
1139 * greater than a valid time, but less than 0xc0 shouldn't appear.
1140 */
1141 seq_puts(seq, "alarm\t\t: ");
1142 if (tm.tm_hour <= 24)
1143 seq_printf(seq, "%02d:", tm.tm_hour);
1144 else
1145 seq_puts(seq, "**:");
1146
1147 if (tm.tm_min <= 59)
1148 seq_printf(seq, "%02d:", tm.tm_min);
1149 else
1150 seq_puts(seq, "**:");
1151
1152 if (tm.tm_sec <= 59)
1153 seq_printf(seq, "%02d\n", tm.tm_sec);
1154 else
1155 seq_puts(seq, "**\n");
1156
1157 seq_printf(seq,
1158 "DST_enable\t: %s\n"
1159 "BCD\t\t: %s\n"
1160 "24hr\t\t: %s\n"
1161 "square_wave\t: %s\n"
1162 "alarm_IRQ\t: %s\n"
1163 "update_IRQ\t: %s\n"
1164 "periodic_IRQ\t: %s\n"
1165 "periodic_freq\t: %ld\n"
1166 "batt_status\t: %s\n",
1167 YN(RTC_DST_EN),
1168 NY(RTC_DM_BINARY),
1169 YN(RTC_24H),
1170 YN(RTC_SQWE),
1171 YN(RTC_AIE),
1172 YN(RTC_UIE),
1173 YN(RTC_PIE),
1174 freq,
1175 batt ? "okay" : "dead");
1176
1177 return 0;
1178#undef YN
1179#undef NY
1180}
Jan Beulich9cef7792006-12-13 00:35:05 -08001181#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Adrian Bunk9580d852008-07-25 19:46:25 -07001183static void rtc_get_rtc_time(struct rtc_time *rtc_tm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
Ingo Molnar0f749642006-07-12 09:03:10 -07001185 unsigned long uip_watchdog = jiffies, flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 unsigned char ctrl;
1187#ifdef CONFIG_MACH_DECSTATION
1188 unsigned int real_year;
1189#endif
1190
1191 /*
1192 * read RTC once any update in progress is done. The update
Luca Falavigna47f176f2005-06-28 20:44:42 -07001193 * can take just over 2ms. We wait 20ms. There is no need to
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
1195 * If you need to know *exactly* when a second has started, enable
Ingo Molnar5fd1fe92008-01-30 13:31:09 +01001196 * periodic update complete interrupts, (via ioctl) and then
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 * immediately read /dev/rtc which will block until you get the IRQ.
1198 * Once the read clears, read the RTC time (again via ioctl). Easy.
1199 */
1200
Julia Lawalldca03a52008-04-28 02:11:59 -07001201 while (rtc_is_updating() != 0 &&
1202 time_before(jiffies, uip_watchdog + 2*HZ/100))
Petr Vandrovec403fe5a2005-08-05 15:50:07 +02001203 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
1205 /*
1206 * Only the values that we read from the RTC are set. We leave
Alan Coxb7599582006-01-11 12:17:32 -08001207 * tm_wday, tm_yday and tm_isdst untouched. Note that while the
1208 * RTC has RTC_DAY_OF_WEEK, we should usually ignore it, as it is
1209 * only updated by the RTC when initially set to a non-zero value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 */
Ingo Molnar0f749642006-07-12 09:03:10 -07001211 spin_lock_irqsave(&rtc_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 rtc_tm->tm_sec = CMOS_READ(RTC_SECONDS);
1213 rtc_tm->tm_min = CMOS_READ(RTC_MINUTES);
1214 rtc_tm->tm_hour = CMOS_READ(RTC_HOURS);
1215 rtc_tm->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH);
1216 rtc_tm->tm_mon = CMOS_READ(RTC_MONTH);
1217 rtc_tm->tm_year = CMOS_READ(RTC_YEAR);
Alan Coxb7599582006-01-11 12:17:32 -08001218 /* Only set from 2.6.16 onwards */
1219 rtc_tm->tm_wday = CMOS_READ(RTC_DAY_OF_WEEK);
1220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221#ifdef CONFIG_MACH_DECSTATION
1222 real_year = CMOS_READ(RTC_DEC_YEAR);
1223#endif
1224 ctrl = CMOS_READ(RTC_CONTROL);
Ingo Molnar0f749642006-07-12 09:03:10 -07001225 spin_unlock_irqrestore(&rtc_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Ingo Molnar5fd1fe92008-01-30 13:31:09 +01001227 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
Adrian Bunk357c6e62008-10-18 20:28:42 -07001228 rtc_tm->tm_sec = bcd2bin(rtc_tm->tm_sec);
1229 rtc_tm->tm_min = bcd2bin(rtc_tm->tm_min);
1230 rtc_tm->tm_hour = bcd2bin(rtc_tm->tm_hour);
1231 rtc_tm->tm_mday = bcd2bin(rtc_tm->tm_mday);
1232 rtc_tm->tm_mon = bcd2bin(rtc_tm->tm_mon);
1233 rtc_tm->tm_year = bcd2bin(rtc_tm->tm_year);
1234 rtc_tm->tm_wday = bcd2bin(rtc_tm->tm_wday);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 }
1236
1237#ifdef CONFIG_MACH_DECSTATION
1238 rtc_tm->tm_year += real_year - 72;
1239#endif
1240
1241 /*
1242 * Account for differences between how the RTC uses the values
1243 * and how they are defined in a struct rtc_time;
1244 */
Ingo Molnar5fd1fe92008-01-30 13:31:09 +01001245 rtc_tm->tm_year += epoch - 1900;
1246 if (rtc_tm->tm_year <= 69)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 rtc_tm->tm_year += 100;
1248
1249 rtc_tm->tm_mon--;
1250}
1251
1252static void get_rtc_alm_time(struct rtc_time *alm_tm)
1253{
1254 unsigned char ctrl;
1255
1256 /*
1257 * Only the values that we read from the RTC are set. That
1258 * means only tm_hour, tm_min, and tm_sec.
1259 */
1260 spin_lock_irq(&rtc_lock);
1261 alm_tm->tm_sec = CMOS_READ(RTC_SECONDS_ALARM);
1262 alm_tm->tm_min = CMOS_READ(RTC_MINUTES_ALARM);
1263 alm_tm->tm_hour = CMOS_READ(RTC_HOURS_ALARM);
1264 ctrl = CMOS_READ(RTC_CONTROL);
1265 spin_unlock_irq(&rtc_lock);
1266
Ingo Molnar5fd1fe92008-01-30 13:31:09 +01001267 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
Adrian Bunk357c6e62008-10-18 20:28:42 -07001268 alm_tm->tm_sec = bcd2bin(alm_tm->tm_sec);
1269 alm_tm->tm_min = bcd2bin(alm_tm->tm_min);
1270 alm_tm->tm_hour = bcd2bin(alm_tm->tm_hour);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 }
1272}
1273
1274#ifdef RTC_IRQ
1275/*
1276 * Used to disable/enable interrupts for any one of UIE, AIE, PIE.
1277 * Rumour has it that if you frob the interrupt enable/disable
1278 * bits in RTC_CONTROL, you should read RTC_INTR_FLAGS, to
1279 * ensure you actually start getting interrupts. Probably for
1280 * compatibility with older/broken chipset RTC implementations.
1281 * We also clear out any old irq data after an ioctl() that
1282 * meddles with the interrupt enable/disable bits.
1283 */
1284
Takashi Iwaic3348762005-11-07 11:14:57 +01001285static void mask_rtc_irq_bit_locked(unsigned char bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
1287 unsigned char val;
1288
Takashi Iwaic3348762005-11-07 11:14:57 +01001289 if (hpet_mask_rtc_irq_bit(bit))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 val = CMOS_READ(RTC_CONTROL);
1292 val &= ~bit;
1293 CMOS_WRITE(val, RTC_CONTROL);
1294 CMOS_READ(RTC_INTR_FLAGS);
1295
1296 rtc_irq_data = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297}
1298
Takashi Iwaic3348762005-11-07 11:14:57 +01001299static void set_rtc_irq_bit_locked(unsigned char bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
1301 unsigned char val;
1302
Takashi Iwaic3348762005-11-07 11:14:57 +01001303 if (hpet_set_rtc_irq_bit(bit))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 val = CMOS_READ(RTC_CONTROL);
1306 val |= bit;
1307 CMOS_WRITE(val, RTC_CONTROL);
1308 CMOS_READ(RTC_INTR_FLAGS);
1309
1310 rtc_irq_data = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311}
1312#endif
1313
1314MODULE_AUTHOR("Paul Gortmaker");
1315MODULE_LICENSE("GPL");
1316MODULE_ALIAS_MISCDEV(RTC_MINOR);