File: /usr/src/linux/drivers/sgi/char/ds1286.c

1     /*
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/types.h>
30     #include <linux/errno.h>
31     #include <linux/miscdevice.h>
32     #include <linux/slab.h>
33     #include <linux/ioport.h>
34     #include <linux/fcntl.h>
35     #include <linux/init.h>
36     #include <linux/poll.h>
37     #include <linux/rtc.h>
38     #include <linux/spinlock.h>
39     
40     #include <asm/ds1286.h>
41     #include <asm/io.h>
42     #include <asm/uaccess.h>
43     #include <asm/system.h>
44     
45     #define DS1286_VERSION		"1.0"
46     
47     /*
48      *	We sponge a minor off of the misc major. No need slurping
49      *	up another valuable major dev number for this. If you add
50      *	an ioctl, make sure you don't conflict with SPARC's RTC
51      *	ioctls.
52      */
53     
54     static DECLARE_WAIT_QUEUE_HEAD(ds1286_wait);
55     
56     static ssize_t ds1286_read(struct file *file, char *buf,
57     			size_t count, loff_t *ppos);
58     
59     static int ds1286_ioctl(struct inode *inode, struct file *file,
60                             unsigned int cmd, unsigned long arg);
61     
62     static unsigned int ds1286_poll(struct file *file, poll_table *wait);
63     
64     void get_rtc_time (struct rtc_time *rtc_tm);
65     void get_rtc_alm_time (struct rtc_time *alm_tm);
66     
67     void set_rtc_irq_bit(unsigned char bit);
68     void clear_rtc_irq_bit(unsigned char bit);
69     
70     static inline unsigned char ds1286_is_updating(void);
71     
72     static spinlock_t ds1286_lock = SPIN_LOCK_UNLOCKED;
73     
74     /*
75      *	Bits in rtc_status. (7 bits of room for future expansion)
76      */
77     
78     #define RTC_IS_OPEN		0x01	/* means /dev/rtc is in use	*/
79     #define RTC_TIMER_ON		0x02	/* missed irq timer active	*/
80     
81     unsigned char ds1286_status;		/* bitmapped status byte.	*/
82     unsigned long ds1286_freq;		/* Current periodic IRQ rate	*/
83     
84     unsigned char days_in_mo[] = 
85     {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
86     
87     /*
88      *	Now all the various file operations that we export.
89      */
90     
91     static ssize_t ds1286_read(struct file *file, char *buf,
92                                size_t count, loff_t *ppos)
93     {
94     	return -EIO;
95     }
96     
97     static int ds1286_ioctl(struct inode *inode, struct file *file,
98                             unsigned int cmd, unsigned long arg)
99     {
100     
101     	struct rtc_time wtime; 
102     
103     	switch (cmd) {
104     	case RTC_AIE_OFF:	/* Mask alarm int. enab. bit	*/
105     	{
106     		unsigned int flags;
107     		unsigned char val;
108     
109     		if (!capable(CAP_SYS_TIME))
110     			return -EACCES;
111     
112     		spin_lock_irqsave(&ds1286_lock, flags);
113     		val = CMOS_READ(RTC_CMD);
114     		val |=  RTC_TDM;
115     		CMOS_WRITE(val, RTC_CMD);
116     		spin_unlock_irqrestore(&ds1286_lock, flags);
117     
118     		return 0;
119     	}
120     	case RTC_AIE_ON:	/* Allow alarm interrupts.	*/
121     	{
122     		unsigned int flags;
123     		unsigned char val;
124     
125     		if (!capable(CAP_SYS_TIME))
126     			return -EACCES;
127     
128     		spin_lock_irqsave(&ds1286_lock, flags);
129     		val = CMOS_READ(RTC_CMD);
130     		val &=  ~RTC_TDM;
131     		CMOS_WRITE(val, RTC_CMD);
132     		spin_unlock_irqrestore(&ds1286_lock, flags);
133     
134     		return 0;
135     	}
136     	case RTC_WIE_OFF:	/* Mask watchdog int. enab. bit	*/
137     	{
138     		unsigned int flags;
139     		unsigned char val;
140     
141     		if (!capable(CAP_SYS_TIME))
142     			return -EACCES;
143     
144     		spin_lock_irqsave(&ds1286_lock, flags);
145     		val = CMOS_READ(RTC_CMD);
146     		val |= RTC_WAM;
147     		CMOS_WRITE(val, RTC_CMD);
148     		spin_unlock_irqrestore(&ds1286_lock, flags);
149     
150     		return 0;
151     	}
152     	case RTC_WIE_ON:	/* Allow watchdog interrupts.	*/
153     	{
154     		unsigned int flags;
155     		unsigned char val;
156     
157     		if (!capable(CAP_SYS_TIME))
158     			return -EACCES;
159     
160     		spin_lock_irqsave(&ds1286_lock, flags);
161     		val = CMOS_READ(RTC_CMD);
162     		val &= ~RTC_WAM;
163     		CMOS_WRITE(val, RTC_CMD);
164     		spin_unlock_irqrestore(&ds1286_lock, flags);
165     
166     		return 0;
167     	}
168     	case RTC_ALM_READ:	/* Read the present alarm time */
169     	{
170     		/*
171     		 * This returns a struct rtc_time. Reading >= 0xc0
172     		 * means "don't care" or "match all". Only the tm_hour,
173     		 * tm_min, and tm_sec values are filled in.
174     		 */
175     
176     		get_rtc_alm_time(&wtime);
177     		break; 
178     	}
179     	case RTC_ALM_SET:	/* Store a time into the alarm */
180     	{
181     		/*
182     		 * This expects a struct rtc_time. Writing 0xff means
183     		 * "don't care" or "match all". Only the tm_hour,
184     		 * tm_min and tm_sec are used.
185     		 */
186     		unsigned char hrs, min, sec;
187     		struct rtc_time alm_tm;
188     
189     		if (!capable(CAP_SYS_TIME))
190     			return -EACCES;
191     
192     		if (copy_from_user(&alm_tm, (struct rtc_time*)arg,
193     				   sizeof(struct rtc_time)))
194     			return -EFAULT;
195     
196     		hrs = alm_tm.tm_hour;
197     		min = alm_tm.tm_min;
198     
199     		if (hrs >= 24)
200     			hrs = 0xff;
201     
202     		if (min >= 60)
203     			min = 0xff;
204     
205     		BIN_TO_BCD(sec);
206     		BIN_TO_BCD(min);
207     		BIN_TO_BCD(hrs);
208     
209     		spin_lock(&ds1286_lock);
210     		CMOS_WRITE(hrs, RTC_HOURS_ALARM);
211     		CMOS_WRITE(min, RTC_MINUTES_ALARM);
212     		spin_unlock(&ds1286_lock);
213     
214     		return 0;
215     	}
216     	case RTC_RD_TIME:	/* Read the time/date from RTC	*/
217     	{
218     		get_rtc_time(&wtime);
219     		break;
220     	}
221     	case RTC_SET_TIME:	/* Set the RTC */
222     	{
223     		struct rtc_time rtc_tm;
224     		unsigned char mon, day, hrs, min, sec, leap_yr;
225     		unsigned char save_control;
226     		unsigned int yrs, flags;
227     
228     		if (!capable(CAP_SYS_TIME))
229     			return -EACCES;
230     
231     		if (copy_from_user(&rtc_tm, (struct rtc_time*)arg,
232     				   sizeof(struct rtc_time)))
233     			return -EFAULT;
234     
235     		yrs = rtc_tm.tm_year + 1900;
236     		mon = rtc_tm.tm_mon + 1;   /* tm_mon starts at zero */
237     		day = rtc_tm.tm_mday;
238     		hrs = rtc_tm.tm_hour;
239     		min = rtc_tm.tm_min;
240     		sec = rtc_tm.tm_sec;
241     
242     		if (yrs < 1970)
243     			return -EINVAL;
244     
245     		leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
246     
247     		if ((mon > 12) || (day == 0))
248     			return -EINVAL;
249     
250     		if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
251     			return -EINVAL;
252     
253     		if ((hrs >= 24) || (min >= 60) || (sec >= 60))
254     			return -EINVAL;
255     
256     		if ((yrs -= 1940) > 255)    /* They are unsigned */
257     			return -EINVAL;
258     
259     		if (yrs >= 100)
260     			yrs -= 100;
261     
262     		BIN_TO_BCD(sec);
263     		BIN_TO_BCD(min);
264     		BIN_TO_BCD(hrs);
265     		BIN_TO_BCD(day);
266     		BIN_TO_BCD(mon);
267     		BIN_TO_BCD(yrs);
268     
269     		spin_lock_irqsave(&ds1286_lock, flags);
270     		save_control = CMOS_READ(RTC_CMD);
271     		CMOS_WRITE((save_control|RTC_TE), RTC_CMD);
272     
273     		CMOS_WRITE(yrs, RTC_YEAR);
274     		CMOS_WRITE(mon, RTC_MONTH);
275     		CMOS_WRITE(day, RTC_DATE);
276     		CMOS_WRITE(hrs, RTC_HOURS);
277     		CMOS_WRITE(min, RTC_MINUTES);
278     		CMOS_WRITE(sec, RTC_SECONDS);
279     		CMOS_WRITE(0, RTC_HUNDREDTH_SECOND);
280     
281     		CMOS_WRITE(save_control, RTC_CMD);
282     		spin_unlock_irqrestore(&ds1286_lock, flags);
283     
284     		return 0;
285     	}
286     	default:
287     		return -EINVAL;
288     	}
289     	return copy_to_user((void *)arg, &wtime, sizeof wtime) ? -EFAULT : 0;
290     }
291     
292     /*
293      *	We enforce only one user at a time here with the open/close.
294      *	Also clear the previous interrupt data on an open, and clean
295      *	up things on a close.
296      */
297     
298     static int ds1286_open(struct inode *inode, struct file *file)
299     {
300     	spin_lock_irq(&ds1286_lock);
301     
302     	if (ds1286_status & RTC_IS_OPEN)
303     		goto out_busy;
304     
305     	ds1286_status |= RTC_IS_OPEN;
306     
307     	spin_lock_irq(&ds1286_lock);
308     	return 0;
309     
310     out_busy:
311     	spin_lock_irq(&ds1286_lock);
312     	return -EBUSY;
313     }
314     
315     static int ds1286_release(struct inode *inode, struct file *file)
316     {
317     	ds1286_status &= ~RTC_IS_OPEN;
318     
319     	return 0;
320     }
321     
322     static unsigned int ds1286_poll(struct file *file, poll_table *wait)
323     {
324     	poll_wait(file, &ds1286_wait, wait);
325     
326     	return 0;
327     }
328     
329     /*
330      *	The various file operations we support.
331      */
332     
333     static struct file_operations ds1286_fops = {
334     	llseek:		no_llseek,
335     	read:		ds1286_read,
336     	poll:		ds1286_poll,
337     	ioctl:		ds1286_ioctl,
338     	open:		ds1286_open,
339     	release:	ds1286_release,
340     };
341     
342     static struct miscdevice ds1286_dev=
343     {
344     	RTC_MINOR,
345     	"rtc",
346     	&ds1286_fops
347     };
348     
349     int __init ds1286_init(void)
350     {
351     	printk(KERN_INFO "DS1286 Real Time Clock Driver v%s\n", DS1286_VERSION);
352     	misc_register(&ds1286_dev);
353     
354     	return 0;
355     }
356     
357     static char *days[] = {
358     	"***", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
359     };
360     
361     /*
362      *	Info exported via "/proc/rtc".
363      */
364     int get_ds1286_status(char *buf)
365     {
366     	char *p, *s;
367     	struct rtc_time tm;
368     	unsigned char hundredth, month, cmd, amode;
369     
370     	p = buf;
371     
372     	get_rtc_time(&tm);
373     	hundredth = CMOS_READ(RTC_HUNDREDTH_SECOND);
374     	BCD_TO_BIN(hundredth);
375     
376     	p += sprintf(p,
377     	             "rtc_time\t: %02d:%02d:%02d.%02d\n"
378     	             "rtc_date\t: %04d-%02d-%02d\n",
379     		     tm.tm_hour, tm.tm_min, tm.tm_sec, hundredth,
380     		     tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
381     
382     	/*
383     	 * We implicitly assume 24hr mode here. Alarm values >= 0xc0 will
384     	 * match any value for that particular field. Values that are
385     	 * greater than a valid time, but less than 0xc0 shouldn't appear.
386     	 */
387     	get_rtc_alm_time(&tm);
388     	p += sprintf(p, "alarm\t\t: %s ", days[tm.tm_wday]);
389     	if (tm.tm_hour <= 24)
390     		p += sprintf(p, "%02d:", tm.tm_hour);
391     	else
392     		p += sprintf(p, "**:");
393     
394     	if (tm.tm_min <= 59)
395     		p += sprintf(p, "%02d\n", tm.tm_min);
396     	else
397     		p += sprintf(p, "**\n");
398     
399     	month = CMOS_READ(RTC_MONTH);
400     	p += sprintf(p,
401     	             "oscillator\t: %s\n"
402     	             "square_wave\t: %s\n",
403     	             (month & RTC_EOSC) ? "disabled" : "enabled",
404     	             (month & RTC_ESQW) ? "disabled" : "enabled");
405     
406     	amode = ((CMOS_READ(RTC_MINUTES_ALARM) & 0x80) >> 5) |
407     	        ((CMOS_READ(RTC_HOURS_ALARM) & 0x80) >> 6) |
408     	        ((CMOS_READ(RTC_DAY_ALARM) & 0x80) >> 7);
409     	if (amode == 7)      s = "each minute";
410     	else if (amode == 3) s = "minutes match";
411     	else if (amode == 1) s = "hours and minutes match";
412     	else if (amode == 0) s = "days, hours and minutes match";
413     	else                 s = "invalid";
414     	p += sprintf(p, "alarm_mode\t: %s\n", s);
415     
416     	cmd = CMOS_READ(RTC_CMD);
417     	p += sprintf(p,
418     	             "alarm_enable\t: %s\n"
419     	             "wdog_alarm\t: %s\n"
420     	             "alarm_mask\t: %s\n"
421     	             "wdog_alarm_mask\t: %s\n"
422     	             "interrupt_mode\t: %s\n"
423     	             "INTB_mode\t: %s_active\n"
424     	             "interrupt_pins\t: %s\n",
425     		     (cmd & RTC_TDF) ? "yes" : "no",
426     		     (cmd & RTC_WAF) ? "yes" : "no",
427     		     (cmd & RTC_TDM) ? "disabled" : "enabled",
428     		     (cmd & RTC_WAM) ? "disabled" : "enabled",
429     		     (cmd & RTC_PU_LVL) ? "pulse" : "level",
430     		     (cmd & RTC_IBH_LO) ? "low" : "high",
431     	             (cmd & RTC_IPSW) ? "unswapped" : "swapped");
432     
433     	return  p - buf;
434     }
435     
436     /*
437      * Returns true if a clock update is in progress
438      */
439     static inline unsigned char ds1286_is_updating(void)
440     {
441     	return CMOS_READ(RTC_CMD) & RTC_TE;
442     }
443     
444     void get_rtc_time(struct rtc_time *rtc_tm)
445     {
446     	unsigned long uip_watchdog = jiffies;
447     	unsigned char save_control;
448     	unsigned int flags;
449     
450     	/*
451     	 * read RTC once any update in progress is done. The update
452     	 * can take just over 2ms. We wait 10 to 20ms. There is no need to
453     	 * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
454     	 * If you need to know *exactly* when a second has started, enable
455     	 * periodic update complete interrupts, (via ioctl) and then 
456     	 * immediately read /dev/rtc which will block until you get the IRQ.
457     	 * Once the read clears, read the RTC time (again via ioctl). Easy.
458     	 */
459     
460     	if (ds1286_is_updating() != 0)
461     		while (jiffies - uip_watchdog < 2*HZ/100)
462     			barrier();
463     
464     	/*
465     	 * Only the values that we read from the RTC are set. We leave
466     	 * tm_wday, tm_yday and tm_isdst untouched. Even though the
467     	 * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
468     	 * by the RTC when initially set to a non-zero value.
469     	 */
470     	spin_lock_irqsave(&ds1286_lock, flags);
471     	save_control = CMOS_READ(RTC_CMD);
472     	CMOS_WRITE((save_control|RTC_TE), RTC_CMD);
473     
474     	rtc_tm->tm_sec = CMOS_READ(RTC_SECONDS);
475     	rtc_tm->tm_min = CMOS_READ(RTC_MINUTES);
476     	rtc_tm->tm_hour = CMOS_READ(RTC_HOURS) & 0x1f;
477     	rtc_tm->tm_mday = CMOS_READ(RTC_DATE);
478     	rtc_tm->tm_mon = CMOS_READ(RTC_MONTH) & 0x1f;
479     	rtc_tm->tm_year = CMOS_READ(RTC_YEAR);
480     
481     	CMOS_WRITE(save_control, RTC_CMD);
482     	spin_unlock_irqrestore(&ds1286_lock, flags);
483     
484     	BCD_TO_BIN(rtc_tm->tm_sec);
485     	BCD_TO_BIN(rtc_tm->tm_min);
486     	BCD_TO_BIN(rtc_tm->tm_hour);
487     	BCD_TO_BIN(rtc_tm->tm_mday);
488     	BCD_TO_BIN(rtc_tm->tm_mon);
489     	BCD_TO_BIN(rtc_tm->tm_year);
490     
491     	/*
492     	 * Account for differences between how the RTC uses the values
493     	 * and how they are defined in a struct rtc_time;
494     	 */
495     	if (rtc_tm->tm_year < 45)
496     		rtc_tm->tm_year += 30;
497     	if ((rtc_tm->tm_year += 40) < 70)
498     		rtc_tm->tm_year += 100;
499     
500     	rtc_tm->tm_mon--;
501     }
502     
503     void get_rtc_alm_time(struct rtc_time *alm_tm)
504     {
505     	unsigned char cmd;
506     	unsigned int flags;
507     
508     	/*
509     	 * Only the values that we read from the RTC are set. That
510     	 * means only tm_wday, tm_hour, tm_min.
511     	 */
512     	spin_lock_irqsave(&ds1286_lock, flags);
513     	alm_tm->tm_min = CMOS_READ(RTC_MINUTES_ALARM) & 0x7f;
514     	alm_tm->tm_hour = CMOS_READ(RTC_HOURS_ALARM)  & 0x1f;
515     	alm_tm->tm_wday = CMOS_READ(RTC_DAY_ALARM)    & 0x07;
516     	cmd = CMOS_READ(RTC_CMD);
517     	spin_unlock_irqrestore(&ds1286_lock, flags);
518     
519     	BCD_TO_BIN(alm_tm->tm_min);
520     	BCD_TO_BIN(alm_tm->tm_hour);
521     	alm_tm->tm_sec = 0;
522     }
523