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/mostek.h>
24 #include <asm/system.h>
25 #include <asm/uaccess.h>
26 #include <asm/rtc.h>
27
28 static int rtc_busy = 0;
29
30 /* Retrieve the current date and time from the real time clock. */
get_rtc_time(struct rtc_time * t)31 void get_rtc_time(struct rtc_time *t)
32 {
33 unsigned long regs = mstk48t02_regs;
34 u8 tmp;
35
36 spin_lock_irq(&mostek_lock);
37
38 tmp = mostek_read(regs + MOSTEK_CREG);
39 tmp |= MSTK_CREG_READ;
40 mostek_write(regs + MOSTEK_CREG, tmp);
41
42 t->sec = MSTK_REG_SEC(regs);
43 t->min = MSTK_REG_MIN(regs);
44 t->hour = MSTK_REG_HOUR(regs);
45 t->dow = MSTK_REG_DOW(regs);
46 t->dom = MSTK_REG_DOM(regs);
47 t->month = MSTK_REG_MONTH(regs);
48 t->year = MSTK_CVT_YEAR( MSTK_REG_YEAR(regs) );
49
50 tmp = mostek_read(regs + MOSTEK_CREG);
51 tmp &= ~MSTK_CREG_READ;
52 mostek_write(regs + MOSTEK_CREG, tmp);
53
54 spin_unlock_irq(&mostek_lock);
55 }
56
57 /* Set the current date and time inthe real time clock. */
set_rtc_time(struct rtc_time * t)58 void set_rtc_time(struct rtc_time *t)
59 {
60 unsigned long regs = mstk48t02_regs;
61 u8 tmp;
62
63 spin_lock_irq(&mostek_lock);
64
65 tmp = mostek_read(regs + MOSTEK_CREG);
66 tmp |= MSTK_CREG_WRITE;
67 mostek_write(regs + MOSTEK_CREG, tmp);
68
69 MSTK_SET_REG_SEC(regs,t->sec);
70 MSTK_SET_REG_MIN(regs,t->min);
71 MSTK_SET_REG_HOUR(regs,t->hour);
72 MSTK_SET_REG_DOW(regs,t->dow);
73 MSTK_SET_REG_DOM(regs,t->dom);
74 MSTK_SET_REG_MONTH(regs,t->month);
75 MSTK_SET_REG_YEAR(regs,t->year - MSTK_YEAR_ZERO);
76
77 tmp = mostek_read(regs + MOSTEK_CREG);
78 tmp &= ~MSTK_CREG_WRITE;
79 mostek_write(regs + MOSTEK_CREG, tmp);
80
81 spin_unlock_irq(&mostek_lock);
82 }
83
rtc_ioctl(struct inode * inode,struct file * file,unsigned int cmd,unsigned long arg)84 static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
85 unsigned long arg)
86 {
87 struct rtc_time rtc_tm;
88
89 switch (cmd)
90 {
91 case RTCGET:
92 memset(&rtc_tm, 0, sizeof(struct rtc_time));
93 get_rtc_time(&rtc_tm);
94
95 if (copy_to_user((struct rtc_time*)arg, &rtc_tm, sizeof(struct rtc_time)))
96 return -EFAULT;
97
98 return 0;
99
100
101 case RTCSET:
102 if (!capable(CAP_SYS_TIME))
103 return -EPERM;
104
105 if (copy_from_user(&rtc_tm, (struct rtc_time*)arg, sizeof(struct rtc_time)))
106 return -EFAULT;
107
108 set_rtc_time(&rtc_tm);
109
110 return 0;
111
112 default:
113 return -EINVAL;
114 }
115 }
116
rtc_open(struct inode * inode,struct file * file)117 static int rtc_open(struct inode *inode, struct file *file)
118 {
119 int ret;
120
121 spin_lock_irq(&mostek_lock);
122 if (rtc_busy) {
123 ret = -EBUSY;
124 } else {
125 rtc_busy = 1;
126 ret = 0;
127 }
128 spin_unlock_irq(&mostek_lock);
129
130 return ret;
131 }
132
rtc_release(struct inode * inode,struct file * file)133 static int rtc_release(struct inode *inode, struct file *file)
134 {
135 rtc_busy = 0;
136
137 return 0;
138 }
139
140 static struct file_operations rtc_fops = {
141 owner: THIS_MODULE,
142 llseek: no_llseek,
143 ioctl: rtc_ioctl,
144 open: rtc_open,
145 release: rtc_release,
146 };
147
148 static struct miscdevice rtc_dev = { RTC_MINOR, "rtc", &rtc_fops };
149
150 EXPORT_NO_SYMBOLS;
151
rtc_sun_init(void)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
rtc_sun_cleanup(void)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");
179