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