vserver 1.9.5.x5
[linux-2.6.git] / drivers / sbus / char / rtc.c
1 /* $Id: rtc.c,v 1.28 2001/10/08 22:19:51 davem Exp $
2  *
3  * Linux/SPARC Real Time Clock Driver
4  * Copyright (C) 1996 Thomas K. Dyas (tdyas@eden.rutgers.edu)
5  *
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.
9  *
10  * Get the modified clock utility from:
11  *   ftp://vger.kernel.org/pub/linux/Sparc/userland/clock.c
12  */
13
14 #include <linux/module.h>
15 #include <linux/types.h>
16 #include <linux/errno.h>
17 #include <linux/miscdevice.h>
18 #include <linux/slab.h>
19 #include <linux/fcntl.h>
20 #include <linux/poll.h>
21 #include <linux/init.h>
22 #include <linux/smp_lock.h>
23 #include <asm/io.h>
24 #include <asm/mostek.h>
25 #include <asm/system.h>
26 #include <asm/uaccess.h>
27 #include <asm/rtc.h>
28
29 static int rtc_busy = 0;
30
31 /* Retrieve the current date and time from the real time clock. */
32 static void get_rtc_time(struct rtc_time *t)
33 {
34         void * __iomem regs = mstk48t02_regs;
35         u8 tmp;
36
37         spin_lock_irq(&mostek_lock);
38
39         tmp = mostek_read(regs + MOSTEK_CREG);
40         tmp |= MSTK_CREG_READ;
41         mostek_write(regs + MOSTEK_CREG, tmp);
42
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) );
50
51         tmp = mostek_read(regs + MOSTEK_CREG);
52         tmp &= ~MSTK_CREG_READ;
53         mostek_write(regs + MOSTEK_CREG, tmp);
54
55         spin_unlock_irq(&mostek_lock);
56 }
57
58 /* Set the current date and time inthe real time clock. */
59 void set_rtc_time(struct rtc_time *t)
60 {
61         void * __iomem regs = mstk48t02_regs;
62         u8 tmp;
63
64         spin_lock_irq(&mostek_lock);
65
66         tmp = mostek_read(regs + MOSTEK_CREG);
67         tmp |= MSTK_CREG_WRITE;
68         mostek_write(regs + MOSTEK_CREG, tmp);
69
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);
77
78         tmp = mostek_read(regs + MOSTEK_CREG);
79         tmp &= ~MSTK_CREG_WRITE;
80         mostek_write(regs + MOSTEK_CREG, tmp);
81
82         spin_unlock_irq(&mostek_lock);
83 }
84
85 static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
86         unsigned long arg)
87 {
88         struct rtc_time rtc_tm;
89         void __user *argp = (void __user *)arg;
90
91         switch (cmd)
92         {
93         case RTCGET:
94                 memset(&rtc_tm, 0, sizeof(struct rtc_time));
95                 get_rtc_time(&rtc_tm);
96
97                 if (copy_to_user(argp, &rtc_tm, sizeof(struct rtc_time)))
98                         return -EFAULT;
99
100                 return 0;
101
102
103         case RTCSET:
104                 if (!capable(CAP_SYS_TIME))
105                         return -EPERM;
106
107                 if (copy_from_user(&rtc_tm, argp, sizeof(struct rtc_time)))
108                         return -EFAULT;
109
110                 set_rtc_time(&rtc_tm);
111
112                 return 0;
113
114         default:
115                 return -EINVAL;
116         }
117 }
118
119 static int rtc_open(struct inode *inode, struct file *file)
120 {
121         int ret;
122
123         spin_lock_irq(&mostek_lock);
124         if (rtc_busy) {
125                 ret = -EBUSY;
126         } else {
127                 rtc_busy = 1;
128                 ret = 0;
129         }
130         spin_unlock_irq(&mostek_lock);
131
132         return ret;
133 }
134
135 static int rtc_release(struct inode *inode, struct file *file)
136 {
137         rtc_busy = 0;
138
139         return 0;
140 }
141
142 static struct file_operations rtc_fops = {
143         .owner =        THIS_MODULE,
144         .llseek =       no_llseek,
145         .ioctl =        rtc_ioctl,
146         .open =         rtc_open,
147         .release =      rtc_release,
148 };
149
150 static struct miscdevice rtc_dev = { RTC_MINOR, "rtc", &rtc_fops };
151
152 static int __init rtc_sun_init(void)
153 {
154         int error;
155
156         /* It is possible we are being driven by some other RTC chip
157          * and thus another RTC driver is handling things.
158          */
159         if (mstk48t02_regs == 0)
160                 return -ENODEV;
161
162         error = misc_register(&rtc_dev);
163         if (error) {
164                 printk(KERN_ERR "rtc: unable to get misc minor for Mostek\n");
165                 return error;
166         }
167
168         return 0;
169 }
170
171 static void __exit rtc_sun_cleanup(void)
172 {
173         misc_deregister(&rtc_dev);
174 }
175
176 module_init(rtc_sun_init);
177 module_exit(rtc_sun_cleanup);
178 MODULE_LICENSE("GPL");