blob: 6ff52bc61325bc500c243ded44c6076415e663e6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Real Time Clock interface for PPC64.
3 *
4 * Based on rtc.c by Paul Gortmaker
5 *
6 * This driver allows use of the real time clock
7 * from user space. It exports the /dev/rtc
8 * interface supporting various ioctl() and also the
9 * /proc/driver/rtc pseudo-file for status information.
10 *
11 * Interface does not support RTC interrupts nor an alarm.
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version
16 * 2 of the License, or (at your option) any later version.
17 *
18 * 1.0 Mike Corrigan: IBM iSeries rtc support
19 * 1.1 Dave Engebretsen: IBM pSeries rtc support
20 */
21
22#define RTC_VERSION "1.1"
23
24#include <linux/config.h>
25#include <linux/module.h>
26#include <linux/kernel.h>
27#include <linux/types.h>
28#include <linux/miscdevice.h>
29#include <linux/ioport.h>
30#include <linux/fcntl.h>
31#include <linux/mc146818rtc.h>
32#include <linux/init.h>
33#include <linux/poll.h>
34#include <linux/proc_fs.h>
35#include <linux/spinlock.h>
36#include <linux/bcd.h>
37#include <linux/interrupt.h>
Nishanth Aravamudan0287ebe2005-09-03 15:56:01 -070038#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40#include <asm/io.h>
41#include <asm/uaccess.h>
42#include <asm/system.h>
43#include <asm/time.h>
44#include <asm/rtas.h>
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/iSeries/mf.h>
47#include <asm/machdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49extern int piranha_simulator;
50
51/*
52 * We sponge a minor off of the misc major. No need slurping
53 * up another valuable major dev number for this. If you add
54 * an ioctl, make sure you don't conflict with SPARC's RTC
55 * ioctls.
56 */
57
58static ssize_t rtc_read(struct file *file, char __user *buf,
59 size_t count, loff_t *ppos);
60
61static int rtc_ioctl(struct inode *inode, struct file *file,
62 unsigned int cmd, unsigned long arg);
63
64static int rtc_read_proc(char *page, char **start, off_t off,
65 int count, int *eof, void *data);
66
67/*
68 * If this driver ever becomes modularised, it will be really nice
69 * to make the epoch retain its value across module reload...
70 */
71
72static unsigned long epoch = 1900; /* year corresponding to 0x00 */
73
74static const unsigned char days_in_mo[] =
75{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
76
77/*
78 * Now all the various file operations that we export.
79 */
80
81static ssize_t rtc_read(struct file *file, char __user *buf,
82 size_t count, loff_t *ppos)
83{
84 return -EIO;
85}
86
87static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
88 unsigned long arg)
89{
90 struct rtc_time wtime;
91
92 switch (cmd) {
93 case RTC_RD_TIME: /* Read the time/date from RTC */
94 {
95 memset(&wtime, 0, sizeof(struct rtc_time));
96 ppc_md.get_rtc_time(&wtime);
97 break;
98 }
99 case RTC_SET_TIME: /* Set the RTC */
100 {
101 struct rtc_time rtc_tm;
102 unsigned char mon, day, hrs, min, sec, leap_yr;
103 unsigned int yrs;
104
105 if (!capable(CAP_SYS_TIME))
106 return -EACCES;
107
108 if (copy_from_user(&rtc_tm, (struct rtc_time __user *)arg,
109 sizeof(struct rtc_time)))
110 return -EFAULT;
111
112 yrs = rtc_tm.tm_year;
113 mon = rtc_tm.tm_mon + 1; /* tm_mon starts at zero */
114 day = rtc_tm.tm_mday;
115 hrs = rtc_tm.tm_hour;
116 min = rtc_tm.tm_min;
117 sec = rtc_tm.tm_sec;
118
119 if (yrs < 70)
120 return -EINVAL;
121
122 leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
123
124 if ((mon > 12) || (day == 0))
125 return -EINVAL;
126
127 if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
128 return -EINVAL;
129
130 if ((hrs >= 24) || (min >= 60) || (sec >= 60))
131 return -EINVAL;
132
133 if ( yrs > 169 )
134 return -EINVAL;
135
136 ppc_md.set_rtc_time(&rtc_tm);
137
138 return 0;
139 }
140 case RTC_EPOCH_READ: /* Read the epoch. */
141 {
142 return put_user (epoch, (unsigned long __user *)arg);
143 }
144 case RTC_EPOCH_SET: /* Set the epoch. */
145 {
146 /*
147 * There were no RTC clocks before 1900.
148 */
149 if (arg < 1900)
150 return -EINVAL;
151
152 if (!capable(CAP_SYS_TIME))
153 return -EACCES;
154
155 epoch = arg;
156 return 0;
157 }
158 default:
159 return -EINVAL;
160 }
161 return copy_to_user((void __user *)arg, &wtime, sizeof wtime) ? -EFAULT : 0;
162}
163
164static int rtc_open(struct inode *inode, struct file *file)
165{
166 nonseekable_open(inode, file);
167 return 0;
168}
169
170static int rtc_release(struct inode *inode, struct file *file)
171{
172 return 0;
173}
174
175/*
176 * The various file operations we support.
177 */
178static struct file_operations rtc_fops = {
179 .owner = THIS_MODULE,
180 .llseek = no_llseek,
181 .read = rtc_read,
182 .ioctl = rtc_ioctl,
183 .open = rtc_open,
184 .release = rtc_release,
185};
186
187static struct miscdevice rtc_dev = {
188 .minor = RTC_MINOR,
189 .name = "rtc",
190 .fops = &rtc_fops
191};
192
193static int __init rtc_init(void)
194{
195 int retval;
196
197 retval = misc_register(&rtc_dev);
198 if(retval < 0)
199 return retval;
200
201#ifdef CONFIG_PROC_FS
202 if (create_proc_read_entry("driver/rtc", 0, NULL, rtc_read_proc, NULL)
203 == NULL) {
204 misc_deregister(&rtc_dev);
205 return -ENOMEM;
206 }
207#endif
208
209 printk(KERN_INFO "i/pSeries Real Time Clock Driver v" RTC_VERSION "\n");
210
211 return 0;
212}
213
214static void __exit rtc_exit (void)
215{
216 remove_proc_entry ("driver/rtc", NULL);
217 misc_deregister(&rtc_dev);
218}
219
220module_init(rtc_init);
221module_exit(rtc_exit);
222
223/*
224 * Info exported via "/proc/driver/rtc".
225 */
226
227static int rtc_proc_output (char *buf)
228{
229
230 char *p;
231 struct rtc_time tm;
232
233 p = buf;
234
235 ppc_md.get_rtc_time(&tm);
236
237 /*
238 * There is no way to tell if the luser has the RTC set for local
239 * time or for Universal Standard Time (GMT). Probably local though.
240 */
241 p += sprintf(p,
242 "rtc_time\t: %02d:%02d:%02d\n"
243 "rtc_date\t: %04d-%02d-%02d\n"
244 "rtc_epoch\t: %04lu\n",
245 tm.tm_hour, tm.tm_min, tm.tm_sec,
246 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, epoch);
247
248 p += sprintf(p,
249 "DST_enable\t: no\n"
250 "BCD\t\t: yes\n"
251 "24hr\t\t: yes\n" );
252
253 return p - buf;
254}
255
256static int rtc_read_proc(char *page, char **start, off_t off,
257 int count, int *eof, void *data)
258{
259 int len = rtc_proc_output (page);
260 if (len <= off+count) *eof = 1;
261 *start = page + off;
262 len -= off;
263 if (len>count) len = count;
264 if (len<0) len = 0;
265 return len;
266}
267
268#ifdef CONFIG_PPC_ISERIES
269/*
270 * Get the RTC from the virtual service processor
271 * This requires flowing LpEvents to the primary partition
272 */
273void iSeries_get_rtc_time(struct rtc_time *rtc_tm)
274{
275 if (piranha_simulator)
276 return;
277
278 mf_get_rtc(rtc_tm);
279 rtc_tm->tm_mon--;
280}
281
282/*
283 * Set the RTC in the virtual service processor
284 * This requires flowing LpEvents to the primary partition
285 */
286int iSeries_set_rtc_time(struct rtc_time *tm)
287{
288 mf_set_rtc(tm);
289 return 0;
290}
291
292void iSeries_get_boot_time(struct rtc_time *tm)
293{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 if ( piranha_simulator )
295 return;
296
Stephen Rothwelld0e8e292005-05-25 16:29:26 +1000297 mf_get_boot_rtc(tm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 tm->tm_mon -= 1;
299}
300#endif
301
302#ifdef CONFIG_PPC_RTAS
303#define MAX_RTC_WAIT 5000 /* 5 sec */
304#define RTAS_CLOCK_BUSY (-2)
Arnd Bergmann773bf9c2005-06-23 09:43:18 +1000305void rtas_get_boot_time(struct rtc_time *rtc_tm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307 int ret[8];
308 int error, wait_time;
309 unsigned long max_wait_tb;
310
311 max_wait_tb = __get_tb() + tb_ticks_per_usec * 1000 * MAX_RTC_WAIT;
312 do {
313 error = rtas_call(rtas_token("get-time-of-day"), 0, 8, ret);
314 if (error == RTAS_CLOCK_BUSY || rtas_is_extended_busy(error)) {
315 wait_time = rtas_extended_busy_delay_time(error);
316 /* This is boot time so we spin. */
317 udelay(wait_time*1000);
318 error = RTAS_CLOCK_BUSY;
319 }
320 } while (error == RTAS_CLOCK_BUSY && (__get_tb() < max_wait_tb));
321
322 if (error != 0 && printk_ratelimit()) {
323 printk(KERN_WARNING "error: reading the clock failed (%d)\n",
324 error);
325 return;
326 }
327
328 rtc_tm->tm_sec = ret[5];
329 rtc_tm->tm_min = ret[4];
330 rtc_tm->tm_hour = ret[3];
331 rtc_tm->tm_mday = ret[2];
332 rtc_tm->tm_mon = ret[1] - 1;
333 rtc_tm->tm_year = ret[0] - 1900;
334}
335
336/* NOTE: get_rtc_time will get an error if executed in interrupt context
337 * and if a delay is needed to read the clock. In this case we just
338 * silently return without updating rtc_tm.
339 */
Arnd Bergmann773bf9c2005-06-23 09:43:18 +1000340void rtas_get_rtc_time(struct rtc_time *rtc_tm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
342 int ret[8];
343 int error, wait_time;
344 unsigned long max_wait_tb;
345
346 max_wait_tb = __get_tb() + tb_ticks_per_usec * 1000 * MAX_RTC_WAIT;
347 do {
348 error = rtas_call(rtas_token("get-time-of-day"), 0, 8, ret);
349 if (error == RTAS_CLOCK_BUSY || rtas_is_extended_busy(error)) {
350 if (in_interrupt() && printk_ratelimit()) {
351 printk(KERN_WARNING "error: reading clock would delay interrupt\n");
352 return; /* delay not allowed */
353 }
354 wait_time = rtas_extended_busy_delay_time(error);
Nishanth Aravamudan0287ebe2005-09-03 15:56:01 -0700355 msleep_interruptible(wait_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 error = RTAS_CLOCK_BUSY;
357 }
358 } while (error == RTAS_CLOCK_BUSY && (__get_tb() < max_wait_tb));
359
360 if (error != 0 && printk_ratelimit()) {
361 printk(KERN_WARNING "error: reading the clock failed (%d)\n",
362 error);
363 return;
364 }
365
366 rtc_tm->tm_sec = ret[5];
367 rtc_tm->tm_min = ret[4];
368 rtc_tm->tm_hour = ret[3];
369 rtc_tm->tm_mday = ret[2];
370 rtc_tm->tm_mon = ret[1] - 1;
371 rtc_tm->tm_year = ret[0] - 1900;
372}
373
Arnd Bergmann773bf9c2005-06-23 09:43:18 +1000374int rtas_set_rtc_time(struct rtc_time *tm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
376 int error, wait_time;
377 unsigned long max_wait_tb;
378
379 max_wait_tb = __get_tb() + tb_ticks_per_usec * 1000 * MAX_RTC_WAIT;
380 do {
381 error = rtas_call(rtas_token("set-time-of-day"), 7, 1, NULL,
382 tm->tm_year + 1900, tm->tm_mon + 1,
383 tm->tm_mday, tm->tm_hour, tm->tm_min,
384 tm->tm_sec, 0);
385 if (error == RTAS_CLOCK_BUSY || rtas_is_extended_busy(error)) {
386 if (in_interrupt())
387 return 1; /* probably decrementer */
388 wait_time = rtas_extended_busy_delay_time(error);
Nishanth Aravamudan0287ebe2005-09-03 15:56:01 -0700389 msleep_interruptible(wait_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 error = RTAS_CLOCK_BUSY;
391 }
392 } while (error == RTAS_CLOCK_BUSY && (__get_tb() < max_wait_tb));
393
394 if (error != 0 && printk_ratelimit())
395 printk(KERN_WARNING "error: setting the clock failed (%d)\n",
396 error);
397
398 return 0;
399}
400#endif