Import 2.3.18pre1
[davej-history.git] / drivers / sbus / char / rtc.c
blob2a6f8624b8166817ef4222ae3dc209f8ce0b4664
1 /* $Id: rtc.c,v 1.18 1999/08/31 18:51:36 davem Exp $
3 * Linux/SPARC Real Time Clock Driver
4 * Copyright (C) 1996 Thomas K. Dyas (tdyas@eden.rutgers.edu)
6 * This is a little driver that lets a user-level program access
7 * the SPARC Mostek real time clock chip. It is no use unless you
8 * use the modified clock utility.
10 * Get the modified clock utility from:
11 * ftp://vger.rutgers.edu/pub/linux/Sparc/userland/clock.c
14 #include <linux/module.h>
15 #include <linux/types.h>
16 #include <linux/errno.h>
17 #include <linux/miscdevice.h>
18 #include <linux/malloc.h>
19 #include <linux/fcntl.h>
20 #include <linux/poll.h>
21 #include <linux/init.h>
22 #include <asm/mostek.h>
23 #include <asm/system.h>
24 #include <asm/uaccess.h>
25 #include <asm/rtc.h>
27 static int rtc_busy = 0;
29 /* Retrieve the current date and time from the real time clock. */
30 void get_rtc_time(struct rtc_time *t)
32 unsigned long regs = mstk48t02_regs;
33 unsigned long flags;
34 u8 tmp;
36 save_flags(flags);
37 cli();
39 tmp = mostek_read(regs + MOSTEK_CREG);
40 tmp |= MSTK_CREG_READ;
41 mostek_write(regs + MOSTEK_CREG, tmp);
43 t->sec = MSTK_REG_SEC(regs);
44 t->min = MSTK_REG_MIN(regs);
45 t->hour = MSTK_REG_HOUR(regs);
46 t->dow = MSTK_REG_DOW(regs);
47 t->dom = MSTK_REG_DOM(regs);
48 t->month = MSTK_REG_MONTH(regs);
49 t->year = MSTK_CVT_YEAR( MSTK_REG_YEAR(regs) );
51 tmp = mostek_read(regs + MOSTEK_CREG);
52 tmp &= ~MSTK_CREG_READ;
53 mostek_write(regs + MOSTEK_CREG, tmp);
54 restore_flags(flags);
57 /* Set the current date and time inthe real time clock. */
58 void set_rtc_time(struct rtc_time *t)
60 unsigned long regs = mstk48t02_regs;
61 unsigned long flags;
62 u8 tmp;
64 save_flags(flags);
65 cli();
66 tmp = mostek_read(regs + MOSTEK_CREG);
67 tmp |= MSTK_CREG_WRITE;
68 mostek_write(regs + MOSTEK_CREG, tmp);
70 MSTK_SET_REG_SEC(regs,t->sec);
71 MSTK_SET_REG_MIN(regs,t->min);
72 MSTK_SET_REG_HOUR(regs,t->hour);
73 MSTK_SET_REG_DOW(regs,t->dow);
74 MSTK_SET_REG_DOM(regs,t->dom);
75 MSTK_SET_REG_MONTH(regs,t->month);
76 MSTK_SET_REG_YEAR(regs,t->year - MSTK_YEAR_ZERO);
78 tmp = mostek_read(regs + MOSTEK_CREG);
79 tmp &= ~MSTK_CREG_WRITE;
80 mostek_write(regs + MOSTEK_CREG, tmp);
81 restore_flags(flags);
84 static long long rtc_lseek(struct file *file, long long offset, int origin)
86 return -ESPIPE;
89 static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
90 unsigned long arg)
92 struct rtc_time rtc_tm;
94 switch (cmd)
96 case RTCGET:
97 get_rtc_time(&rtc_tm);
99 copy_to_user_ret((struct rtc_time*)arg, &rtc_tm, sizeof(struct rtc_time), -EFAULT);
101 return 0;
104 case RTCSET:
105 if (!capable(CAP_SYS_TIME))
106 return -EPERM;
108 copy_from_user_ret(&rtc_tm, (struct rtc_time*)arg, sizeof(struct rtc_time), -EFAULT);
110 set_rtc_time(&rtc_tm);
112 return 0;
114 default:
115 return -EINVAL;
119 static int rtc_open(struct inode *inode, struct file *file)
122 if (rtc_busy)
123 return -EBUSY;
125 rtc_busy = 1;
127 MOD_INC_USE_COUNT;
129 return 0;
132 static int rtc_release(struct inode *inode, struct file *file)
134 MOD_DEC_USE_COUNT;
135 rtc_busy = 0;
136 return 0;
139 static struct file_operations rtc_fops = {
140 rtc_lseek,
141 NULL, /* rtc_read */
142 NULL, /* rtc_write */
143 NULL, /* rtc_readdir */
144 NULL, /* rtc_poll */
145 rtc_ioctl,
146 NULL, /* rtc_mmap */
147 rtc_open,
148 NULL, /* flush */
149 rtc_release
152 static struct miscdevice rtc_dev = { RTC_MINOR, "rtc", &rtc_fops };
154 EXPORT_NO_SYMBOLS;
156 #ifdef MODULE
157 int init_module(void)
158 #else
159 int __init rtc_sun_init(void)
160 #endif
162 int error;
164 if (mstk48t02_regs == 0) {
165 /* This diagnostic is a debugging aid... But a useful one. */
166 printk(KERN_ERR "rtc: no Mostek in this computer\n");
167 return -ENODEV;
170 error = misc_register(&rtc_dev);
171 if (error) {
172 printk(KERN_ERR "rtc: unable to get misc minor for Mostek\n");
173 return error;
176 return 0;
179 #ifdef MODULE
180 void cleanup_module(void)
182 misc_deregister(&rtc_dev);
184 #endif