1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#define RTC_VERSION "1.09b"
26
27#include <linux/bcd.h>
28#include <linux/module.h>
29#include <linux/kernel.h>
30#include <linux/types.h>
31#include <linux/miscdevice.h>
32#include <linux/ioport.h>
33#include <linux/fcntl.h>
34#include <linux/rtc.h>
35#include <linux/init.h>
36#include <linux/poll.h>
37#include <linux/proc_fs.h>
38
39#include <asm/m48t35.h>
40#include <asm/sn/ioc3.h>
41#include <asm/io.h>
42#include <asm/uaccess.h>
43#include <asm/system.h>
44#include <asm/sn/klconfig.h>
45#include <asm/sn/sn0/ip27.h>
46#include <asm/sn/sn0/hub.h>
47#include <asm/sn/sn_private.h>
48
49static int rtc_ioctl(struct inode *inode, struct file *file,
50 unsigned int cmd, unsigned long arg);
51
52static int rtc_read_proc(char *page, char **start, off_t off,
53 int count, int *eof, void *data);
54
55static void get_rtc_time(struct rtc_time *rtc_tm);
56
57
58
59
60
61#define RTC_IS_OPEN 0x01
62#define RTC_TIMER_ON 0x02
63
64static unsigned char rtc_status;
65static unsigned long rtc_freq;
66static struct m48t35_rtc *rtc;
67
68
69
70
71
72
73static unsigned long epoch = 1970;
74
75static const unsigned char days_in_mo[] =
76{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
77
78static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
79 unsigned long arg)
80{
81
82 struct rtc_time wtime;
83
84 switch (cmd) {
85 case RTC_RD_TIME:
86 {
87 get_rtc_time(&wtime);
88 break;
89 }
90 case RTC_SET_TIME:
91 {
92 struct rtc_time rtc_tm;
93 unsigned char mon, day, hrs, min, sec, leap_yr;
94 unsigned int yrs;
95
96 if (!capable(CAP_SYS_TIME))
97 return -EACCES;
98
99 if (copy_from_user(&rtc_tm, (struct rtc_time*)arg,
100 sizeof(struct rtc_time)))
101 return -EFAULT;
102
103 yrs = rtc_tm.tm_year + 1900;
104 mon = rtc_tm.tm_mon + 1;
105 day = rtc_tm.tm_mday;
106 hrs = rtc_tm.tm_hour;
107 min = rtc_tm.tm_min;
108 sec = rtc_tm.tm_sec;
109
110 if (yrs < 1970)
111 return -EINVAL;
112
113 leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
114
115 if ((mon > 12) || (day == 0))
116 return -EINVAL;
117
118 if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
119 return -EINVAL;
120
121 if ((hrs >= 24) || (min >= 60) || (sec >= 60))
122 return -EINVAL;
123
124 if ((yrs -= epoch) > 255)
125 return -EINVAL;
126
127 if (yrs > 169)
128 return -EINVAL;
129
130 if (yrs >= 100)
131 yrs -= 100;
132
133 sec = BIN2BCD(sec);
134 min = BIN2BCD(min);
135 hrs = BIN2BCD(hrs);
136 day = BIN2BCD(day);
137 mon = BIN2BCD(mon);
138 yrs = BIN2BCD(yrs);
139
140 spin_lock_irq(&rtc_lock);
141 rtc->control |= M48T35_RTC_SET;
142 rtc->year = yrs;
143 rtc->month = mon;
144 rtc->date = day;
145 rtc->hour = hrs;
146 rtc->min = min;
147 rtc->sec = sec;
148 rtc->control &= ~M48T35_RTC_SET;
149 spin_unlock_irq(&rtc_lock);
150
151 return 0;
152 }
153 default:
154 return -EINVAL;
155 }
156 return copy_to_user((void *)arg, &wtime, sizeof wtime) ? -EFAULT : 0;
157}
158
159
160
161
162
163
164
165static int rtc_open(struct inode *inode, struct file *file)
166{
167 spin_lock_irq(&rtc_lock);
168
169 if (rtc_status & RTC_IS_OPEN) {
170 spin_unlock_irq(&rtc_lock);
171 return -EBUSY;
172 }
173
174 rtc_status |= RTC_IS_OPEN;
175 spin_unlock_irq(&rtc_lock);
176
177 return 0;
178}
179
180static int rtc_release(struct inode *inode, struct file *file)
181{
182
183
184
185
186
187 spin_lock_irq(&rtc_lock);
188 rtc_status &= ~RTC_IS_OPEN;
189 spin_unlock_irq(&rtc_lock);
190
191 return 0;
192}
193
194
195
196
197
198static const struct file_operations rtc_fops = {
199 .owner = THIS_MODULE,
200 .ioctl = rtc_ioctl,
201 .open = rtc_open,
202 .release = rtc_release,
203};
204
205static struct miscdevice rtc_dev=
206{
207 RTC_MINOR,
208 "rtc",
209 &rtc_fops
210};
211
212static int __init rtc_init(void)
213{
214 rtc = (struct m48t35_rtc *)
215 (KL_CONFIG_CH_CONS_INFO(master_nasid)->memory_base + IOC3_BYTEBUS_DEV0);
216
217 printk(KERN_INFO "Real Time Clock Driver v%s\n", RTC_VERSION);
218 if (misc_register(&rtc_dev)) {
219 printk(KERN_ERR "rtc: cannot register misc device.\n");
220 return -ENODEV;
221 }
222 if (!create_proc_read_entry("driver/rtc", 0, NULL, rtc_read_proc, NULL)) {
223 printk(KERN_ERR "rtc: cannot create /proc/rtc.\n");
224 misc_deregister(&rtc_dev);
225 return -ENOENT;
226 }
227
228 rtc_freq = 1024;
229
230 return 0;
231}
232
233static void __exit rtc_exit (void)
234{
235
236
237 remove_proc_entry ("rtc", NULL);
238 misc_deregister(&rtc_dev);
239}
240
241module_init(rtc_init);
242module_exit(rtc_exit);
243
244
245
246
247
248static int rtc_get_status(char *buf)
249{
250 char *p;
251 struct rtc_time tm;
252
253
254
255
256
257 p = buf;
258
259 get_rtc_time(&tm);
260
261
262
263
264
265 p += sprintf(p,
266 "rtc_time\t: %02d:%02d:%02d\n"
267 "rtc_date\t: %04d-%02d-%02d\n"
268 "rtc_epoch\t: %04lu\n"
269 "24hr\t\t: yes\n",
270 tm.tm_hour, tm.tm_min, tm.tm_sec,
271 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, epoch);
272
273 return p - buf;
274}
275
276static int rtc_read_proc(char *page, char **start, off_t off,
277 int count, int *eof, void *data)
278{
279 int len = rtc_get_status(page);
280 if (len <= off+count) *eof = 1;
281 *start = page + off;
282 len -= off;
283 if (len>count) len = count;
284 if (len<0) len = 0;
285 return len;
286}
287
288static void get_rtc_time(struct rtc_time *rtc_tm)
289{
290
291
292
293
294
295
296
297
298
299
300 spin_lock_irq(&rtc_lock);
301 rtc->control |= M48T35_RTC_READ;
302 rtc_tm->tm_sec = rtc->sec;
303 rtc_tm->tm_min = rtc->min;
304 rtc_tm->tm_hour = rtc->hour;
305 rtc_tm->tm_mday = rtc->date;
306 rtc_tm->tm_mon = rtc->month;
307 rtc_tm->tm_year = rtc->year;
308 rtc->control &= ~M48T35_RTC_READ;
309 spin_unlock_irq(&rtc_lock);
310
311 rtc_tm->tm_sec = BCD2BIN(rtc_tm->tm_sec);
312 rtc_tm->tm_min = BCD2BIN(rtc_tm->tm_min);
313 rtc_tm->tm_hour = BCD2BIN(rtc_tm->tm_hour);
314 rtc_tm->tm_mday = BCD2BIN(rtc_tm->tm_mday);
315 rtc_tm->tm_mon = BCD2BIN(rtc_tm->tm_mon);
316 rtc_tm->tm_year = BCD2BIN(rtc_tm->tm_year);
317
318
319
320
321
322 if ((rtc_tm->tm_year += (epoch - 1900)) <= 69)
323 rtc_tm->tm_year += 100;
324
325 rtc_tm->tm_mon--;
326}
327