]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Driver for the SGS-Thomson M48T35 Timekeeper RAM chip | |
3 | * | |
4 | * Real Time Clock interface for Linux | |
5 | * | |
6 | * TODO: Implement periodic interrupts. | |
7 | * | |
8 | * Copyright (C) 2000 Silicon Graphics, Inc. | |
9 | * Written by Ulf Carlsson ([email protected]) | |
10 | * | |
11 | * Based on code written by Paul Gortmaker. | |
12 | * | |
13 | * This driver allows use of the real time clock (built into | |
14 | * nearly all computers) from user space. It exports the /dev/rtc | |
15 | * interface supporting various ioctl() and also the /proc/rtc | |
16 | * pseudo-file for status information. | |
17 | * | |
18 | * This program is free software; you can redistribute it and/or | |
19 | * modify it under the terms of the GNU General Public License | |
20 | * as published by the Free Software Foundation; either version | |
21 | * 2 of the License, or (at your option) any later version. | |
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 | #include <linux/smp_lock.h> | |
39 | ||
40 | #include <asm/m48t35.h> | |
41 | #include <asm/sn/ioc3.h> | |
42 | #include <asm/io.h> | |
43 | #include <asm/uaccess.h> | |
44 | #include <asm/system.h> | |
45 | #include <asm/sn/klconfig.h> | |
46 | #include <asm/sn/sn0/ip27.h> | |
47 | #include <asm/sn/sn0/hub.h> | |
48 | #include <asm/sn/sn_private.h> | |
49 | ||
50 | static int rtc_ioctl(struct inode *inode, struct file *file, | |
51 | unsigned int cmd, unsigned long arg); | |
52 | ||
53 | static int rtc_read_proc(char *page, char **start, off_t off, | |
54 | int count, int *eof, void *data); | |
55 | ||
56 | static void get_rtc_time(struct rtc_time *rtc_tm); | |
57 | ||
58 | /* | |
59 | * Bits in rtc_status. (6 bits of room for future expansion) | |
60 | */ | |
61 | ||
62 | #define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */ | |
63 | #define RTC_TIMER_ON 0x02 /* missed irq timer active */ | |
64 | ||
65 | static unsigned char rtc_status; /* bitmapped status byte. */ | |
66 | static unsigned long rtc_freq; /* Current periodic IRQ rate */ | |
67 | static struct m48t35_rtc *rtc; | |
68 | ||
69 | /* | |
70 | * If this driver ever becomes modularised, it will be really nice | |
71 | * to make the epoch retain its value across module reload... | |
72 | */ | |
73 | ||
74 | static unsigned long epoch = 1970; /* year corresponding to 0x00 */ | |
75 | ||
76 | static const unsigned char days_in_mo[] = | |
77 | {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; | |
78 | ||
79 | static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd, | |
80 | unsigned long arg) | |
81 | { | |
82 | ||
83 | struct rtc_time wtime; | |
84 | ||
85 | switch (cmd) { | |
86 | case RTC_RD_TIME: /* Read the time/date from RTC */ | |
87 | { | |
88 | get_rtc_time(&wtime); | |
89 | break; | |
90 | } | |
91 | case RTC_SET_TIME: /* Set the RTC */ | |
92 | { | |
93 | struct rtc_time rtc_tm; | |
94 | unsigned char mon, day, hrs, min, sec, leap_yr; | |
95 | unsigned int yrs; | |
96 | ||
97 | if (!capable(CAP_SYS_TIME)) | |
98 | return -EACCES; | |
99 | ||
100 | if (copy_from_user(&rtc_tm, (struct rtc_time*)arg, | |
101 | sizeof(struct rtc_time))) | |
102 | return -EFAULT; | |
103 | ||
104 | yrs = rtc_tm.tm_year + 1900; | |
105 | mon = rtc_tm.tm_mon + 1; /* tm_mon starts at zero */ | |
106 | day = rtc_tm.tm_mday; | |
107 | hrs = rtc_tm.tm_hour; | |
108 | min = rtc_tm.tm_min; | |
109 | sec = rtc_tm.tm_sec; | |
110 | ||
111 | if (yrs < 1970) | |
112 | return -EINVAL; | |
113 | ||
114 | leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400)); | |
115 | ||
116 | if ((mon > 12) || (day == 0)) | |
117 | return -EINVAL; | |
118 | ||
119 | if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr))) | |
120 | return -EINVAL; | |
121 | ||
122 | if ((hrs >= 24) || (min >= 60) || (sec >= 60)) | |
123 | return -EINVAL; | |
124 | ||
125 | if ((yrs -= epoch) > 255) /* They are unsigned */ | |
126 | return -EINVAL; | |
127 | ||
128 | if (yrs > 169) | |
129 | return -EINVAL; | |
130 | ||
131 | if (yrs >= 100) | |
132 | yrs -= 100; | |
133 | ||
134 | sec = BIN2BCD(sec); | |
135 | min = BIN2BCD(min); | |
136 | hrs = BIN2BCD(hrs); | |
137 | day = BIN2BCD(day); | |
138 | mon = BIN2BCD(mon); | |
139 | yrs = BIN2BCD(yrs); | |
140 | ||
141 | spin_lock_irq(&rtc_lock); | |
142 | rtc->control |= M48T35_RTC_SET; | |
143 | rtc->year = yrs; | |
144 | rtc->month = mon; | |
145 | rtc->date = day; | |
146 | rtc->hour = hrs; | |
147 | rtc->min = min; | |
148 | rtc->sec = sec; | |
149 | rtc->control &= ~M48T35_RTC_SET; | |
150 | spin_unlock_irq(&rtc_lock); | |
151 | ||
152 | return 0; | |
153 | } | |
154 | default: | |
155 | return -EINVAL; | |
156 | } | |
157 | return copy_to_user((void *)arg, &wtime, sizeof wtime) ? -EFAULT : 0; | |
158 | } | |
159 | ||
160 | /* | |
161 | * We enforce only one user at a time here with the open/close. | |
162 | * Also clear the previous interrupt data on an open, and clean | |
163 | * up things on a close. | |
164 | */ | |
165 | ||
166 | static int rtc_open(struct inode *inode, struct file *file) | |
167 | { | |
168 | spin_lock_irq(&rtc_lock); | |
169 | ||
170 | if (rtc_status & RTC_IS_OPEN) { | |
171 | spin_unlock_irq(&rtc_lock); | |
172 | return -EBUSY; | |
173 | } | |
174 | ||
175 | rtc_status |= RTC_IS_OPEN; | |
176 | spin_unlock_irq(&rtc_lock); | |
177 | ||
178 | return 0; | |
179 | } | |
180 | ||
181 | static int rtc_release(struct inode *inode, struct file *file) | |
182 | { | |
183 | /* | |
184 | * Turn off all interrupts once the device is no longer | |
185 | * in use, and clear the data. | |
186 | */ | |
187 | ||
188 | spin_lock_irq(&rtc_lock); | |
189 | rtc_status &= ~RTC_IS_OPEN; | |
190 | spin_unlock_irq(&rtc_lock); | |
191 | ||
192 | return 0; | |
193 | } | |
194 | ||
195 | /* | |
196 | * The various file operations we support. | |
197 | */ | |
198 | ||
62322d25 | 199 | static const struct file_operations rtc_fops = { |
1da177e4 LT |
200 | .owner = THIS_MODULE, |
201 | .ioctl = rtc_ioctl, | |
202 | .open = rtc_open, | |
203 | .release = rtc_release, | |
204 | }; | |
205 | ||
206 | static struct miscdevice rtc_dev= | |
207 | { | |
208 | RTC_MINOR, | |
209 | "rtc", | |
210 | &rtc_fops | |
211 | }; | |
212 | ||
213 | static int __init rtc_init(void) | |
214 | { | |
215 | rtc = (struct m48t35_rtc *) | |
216 | (KL_CONFIG_CH_CONS_INFO(master_nasid)->memory_base + IOC3_BYTEBUS_DEV0); | |
217 | ||
218 | printk(KERN_INFO "Real Time Clock Driver v%s\n", RTC_VERSION); | |
219 | if (misc_register(&rtc_dev)) { | |
220 | printk(KERN_ERR "rtc: cannot register misc device.\n"); | |
221 | return -ENODEV; | |
222 | } | |
223 | if (!create_proc_read_entry("driver/rtc", 0, NULL, rtc_read_proc, NULL)) { | |
224 | printk(KERN_ERR "rtc: cannot create /proc/rtc.\n"); | |
225 | misc_deregister(&rtc_dev); | |
226 | return -ENOENT; | |
227 | } | |
228 | ||
229 | rtc_freq = 1024; | |
230 | ||
231 | return 0; | |
232 | } | |
233 | ||
234 | static void __exit rtc_exit (void) | |
235 | { | |
236 | /* interrupts and timer disabled at this point by rtc_release */ | |
237 | ||
238 | remove_proc_entry ("rtc", NULL); | |
239 | misc_deregister(&rtc_dev); | |
240 | } | |
241 | ||
242 | module_init(rtc_init); | |
243 | module_exit(rtc_exit); | |
244 | ||
245 | /* | |
246 | * Info exported via "/proc/rtc". | |
247 | */ | |
248 | ||
249 | static int rtc_get_status(char *buf) | |
250 | { | |
251 | char *p; | |
252 | struct rtc_time tm; | |
253 | ||
254 | /* | |
255 | * Just emulate the standard /proc/rtc | |
256 | */ | |
257 | ||
258 | p = buf; | |
259 | ||
260 | get_rtc_time(&tm); | |
261 | ||
262 | /* | |
263 | * There is no way to tell if the luser has the RTC set for local | |
264 | * time or for Universal Standard Time (GMT). Probably local though. | |
265 | */ | |
266 | p += sprintf(p, | |
267 | "rtc_time\t: %02d:%02d:%02d\n" | |
268 | "rtc_date\t: %04d-%02d-%02d\n" | |
269 | "rtc_epoch\t: %04lu\n" | |
270 | "24hr\t\t: yes\n", | |
271 | tm.tm_hour, tm.tm_min, tm.tm_sec, | |
272 | tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, epoch); | |
273 | ||
274 | return p - buf; | |
275 | } | |
276 | ||
277 | static int rtc_read_proc(char *page, char **start, off_t off, | |
278 | int count, int *eof, void *data) | |
279 | { | |
280 | int len = rtc_get_status(page); | |
281 | if (len <= off+count) *eof = 1; | |
282 | *start = page + off; | |
283 | len -= off; | |
284 | if (len>count) len = count; | |
285 | if (len<0) len = 0; | |
286 | return len; | |
287 | } | |
288 | ||
289 | static void get_rtc_time(struct rtc_time *rtc_tm) | |
290 | { | |
291 | /* | |
292 | * Do we need to wait for the last update to finish? | |
293 | */ | |
294 | ||
295 | /* | |
296 | * Only the values that we read from the RTC are set. We leave | |
297 | * tm_wday, tm_yday and tm_isdst untouched. Even though the | |
298 | * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated | |
299 | * by the RTC when initially set to a non-zero value. | |
300 | */ | |
301 | spin_lock_irq(&rtc_lock); | |
302 | rtc->control |= M48T35_RTC_READ; | |
303 | rtc_tm->tm_sec = rtc->sec; | |
304 | rtc_tm->tm_min = rtc->min; | |
305 | rtc_tm->tm_hour = rtc->hour; | |
306 | rtc_tm->tm_mday = rtc->date; | |
307 | rtc_tm->tm_mon = rtc->month; | |
308 | rtc_tm->tm_year = rtc->year; | |
309 | rtc->control &= ~M48T35_RTC_READ; | |
310 | spin_unlock_irq(&rtc_lock); | |
311 | ||
312 | rtc_tm->tm_sec = BCD2BIN(rtc_tm->tm_sec); | |
313 | rtc_tm->tm_min = BCD2BIN(rtc_tm->tm_min); | |
314 | rtc_tm->tm_hour = BCD2BIN(rtc_tm->tm_hour); | |
315 | rtc_tm->tm_mday = BCD2BIN(rtc_tm->tm_mday); | |
316 | rtc_tm->tm_mon = BCD2BIN(rtc_tm->tm_mon); | |
317 | rtc_tm->tm_year = BCD2BIN(rtc_tm->tm_year); | |
318 | ||
319 | /* | |
320 | * Account for differences between how the RTC uses the values | |
321 | * and how they are defined in a struct rtc_time; | |
322 | */ | |
323 | if ((rtc_tm->tm_year += (epoch - 1900)) <= 69) | |
324 | rtc_tm->tm_year += 100; | |
325 | ||
326 | rtc_tm->tm_mon--; | |
327 | } |