[PATCH] powerpc: Merge thread_info.h
[powerpc.git] / arch / ppc64 / kernel / rtc.c
1 /*
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>
38 #include <linux/delay.h>
39
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
46 #include <asm/machdep.h>
47
48 /*
49  *      We sponge a minor off of the misc major. No need slurping
50  *      up another valuable major dev number for this. If you add
51  *      an ioctl, make sure you don't conflict with SPARC's RTC
52  *      ioctls.
53  */
54
55 static ssize_t rtc_read(struct file *file, char __user *buf,
56                         size_t count, loff_t *ppos);
57
58 static int rtc_ioctl(struct inode *inode, struct file *file,
59                      unsigned int cmd, unsigned long arg);
60
61 static int rtc_read_proc(char *page, char **start, off_t off,
62                          int count, int *eof, void *data);
63
64 /*
65  *      If this driver ever becomes modularised, it will be really nice
66  *      to make the epoch retain its value across module reload...
67  */
68
69 static unsigned long epoch = 1900;      /* year corresponding to 0x00   */
70
71 static const unsigned char days_in_mo[] = 
72 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
73
74 /*
75  *      Now all the various file operations that we export.
76  */
77
78 static ssize_t rtc_read(struct file *file, char __user *buf,
79                         size_t count, loff_t *ppos)
80 {
81         return -EIO;
82 }
83
84 static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
85                      unsigned long arg)
86 {
87         struct rtc_time wtime; 
88
89         switch (cmd) {
90         case RTC_RD_TIME:       /* Read the time/date from RTC  */
91         {
92                 memset(&wtime, 0, sizeof(struct rtc_time));
93                 ppc_md.get_rtc_time(&wtime);
94                 break;
95         }
96         case RTC_SET_TIME:      /* Set the RTC */
97         {
98                 struct rtc_time rtc_tm;
99                 unsigned char mon, day, hrs, min, sec, leap_yr;
100                 unsigned int yrs;
101
102                 if (!capable(CAP_SYS_TIME))
103                         return -EACCES;
104
105                 if (copy_from_user(&rtc_tm, (struct rtc_time __user *)arg,
106                                    sizeof(struct rtc_time)))
107                         return -EFAULT;
108
109                 yrs = rtc_tm.tm_year;
110                 mon = rtc_tm.tm_mon + 1;   /* tm_mon starts at zero */
111                 day = rtc_tm.tm_mday;
112                 hrs = rtc_tm.tm_hour;
113                 min = rtc_tm.tm_min;
114                 sec = rtc_tm.tm_sec;
115
116                 if (yrs < 70)
117                         return -EINVAL;
118
119                 leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
120
121                 if ((mon > 12) || (day == 0))
122                         return -EINVAL;
123
124                 if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
125                         return -EINVAL;
126                         
127                 if ((hrs >= 24) || (min >= 60) || (sec >= 60))
128                         return -EINVAL;
129
130                 if ( yrs > 169 )
131                         return -EINVAL;
132
133                 ppc_md.set_rtc_time(&rtc_tm);
134                 
135                 return 0;
136         }
137         case RTC_EPOCH_READ:    /* Read the epoch.      */
138         {
139                 return put_user (epoch, (unsigned long __user *)arg);
140         }
141         case RTC_EPOCH_SET:     /* Set the epoch.       */
142         {
143                 /* 
144                  * There were no RTC clocks before 1900.
145                  */
146                 if (arg < 1900)
147                         return -EINVAL;
148
149                 if (!capable(CAP_SYS_TIME))
150                         return -EACCES;
151
152                 epoch = arg;
153                 return 0;
154         }
155         default:
156                 return -EINVAL;
157         }
158         return copy_to_user((void __user *)arg, &wtime, sizeof wtime) ? -EFAULT : 0;
159 }
160
161 static int rtc_open(struct inode *inode, struct file *file)
162 {
163         nonseekable_open(inode, file);
164         return 0;
165 }
166
167 static int rtc_release(struct inode *inode, struct file *file)
168 {
169         return 0;
170 }
171
172 /*
173  *      The various file operations we support.
174  */
175 static struct file_operations rtc_fops = {
176         .owner =        THIS_MODULE,
177         .llseek =       no_llseek,
178         .read =         rtc_read,
179         .ioctl =        rtc_ioctl,
180         .open =         rtc_open,
181         .release =      rtc_release,
182 };
183
184 static struct miscdevice rtc_dev = {
185         .minor =        RTC_MINOR,
186         .name =         "rtc",
187         .fops =         &rtc_fops
188 };
189
190 static int __init rtc_init(void)
191 {
192         int retval;
193
194         retval = misc_register(&rtc_dev);
195         if(retval < 0)
196                 return retval;
197
198 #ifdef CONFIG_PROC_FS
199         if (create_proc_read_entry("driver/rtc", 0, NULL, rtc_read_proc, NULL)
200                         == NULL) {
201                 misc_deregister(&rtc_dev);
202                 return -ENOMEM;
203         }
204 #endif
205
206         printk(KERN_INFO "i/pSeries Real Time Clock Driver v" RTC_VERSION "\n");
207
208         return 0;
209 }
210
211 static void __exit rtc_exit (void)
212 {
213         remove_proc_entry ("driver/rtc", NULL);
214         misc_deregister(&rtc_dev);
215 }
216
217 module_init(rtc_init);
218 module_exit(rtc_exit);
219
220 /*
221  *      Info exported via "/proc/driver/rtc".
222  */
223
224 static int rtc_proc_output (char *buf)
225 {
226         
227         char *p;
228         struct rtc_time tm;
229         
230         p = buf;
231
232         ppc_md.get_rtc_time(&tm);
233
234         /*
235          * There is no way to tell if the luser has the RTC set for local
236          * time or for Universal Standard Time (GMT). Probably local though.
237          */
238         p += sprintf(p,
239                      "rtc_time\t: %02d:%02d:%02d\n"
240                      "rtc_date\t: %04d-%02d-%02d\n"
241                      "rtc_epoch\t: %04lu\n",
242                      tm.tm_hour, tm.tm_min, tm.tm_sec,
243                      tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, epoch);
244
245         p += sprintf(p,
246                      "DST_enable\t: no\n"
247                      "BCD\t\t: yes\n"
248                      "24hr\t\t: yes\n" );
249
250         return  p - buf;
251 }
252
253 static int rtc_read_proc(char *page, char **start, off_t off,
254                          int count, int *eof, void *data)
255 {
256         int len = rtc_proc_output (page);
257         if (len <= off+count) *eof = 1;
258         *start = page + off;
259         len -= off;
260         if (len>count) len = count;
261         if (len<0) len = 0;
262         return len;
263 }
264
265 #ifdef CONFIG_PPC_RTAS
266 #define MAX_RTC_WAIT 5000       /* 5 sec */
267 #define RTAS_CLOCK_BUSY (-2)
268 unsigned long rtas_get_boot_time(void)
269 {
270         int ret[8];
271         int error, wait_time;
272         unsigned long max_wait_tb;
273
274         max_wait_tb = __get_tb() + tb_ticks_per_usec * 1000 * MAX_RTC_WAIT;
275         do {
276                 error = rtas_call(rtas_token("get-time-of-day"), 0, 8, ret);
277                 if (error == RTAS_CLOCK_BUSY || rtas_is_extended_busy(error)) {
278                         wait_time = rtas_extended_busy_delay_time(error);
279                         /* This is boot time so we spin. */
280                         udelay(wait_time*1000);
281                         error = RTAS_CLOCK_BUSY;
282                 }
283         } while (error == RTAS_CLOCK_BUSY && (__get_tb() < max_wait_tb));
284
285         if (error != 0 && printk_ratelimit()) {
286                 printk(KERN_WARNING "error: reading the clock failed (%d)\n",
287                         error);
288                 return 0;
289         }
290
291         return mktime(ret[0], ret[1], ret[2], ret[3], ret[4], ret[5]);
292 }
293
294 /* NOTE: get_rtc_time will get an error if executed in interrupt context
295  * and if a delay is needed to read the clock.  In this case we just
296  * silently return without updating rtc_tm.
297  */
298 void rtas_get_rtc_time(struct rtc_time *rtc_tm)
299 {
300         int ret[8];
301         int error, wait_time;
302         unsigned long max_wait_tb;
303
304         max_wait_tb = __get_tb() + tb_ticks_per_usec * 1000 * MAX_RTC_WAIT;
305         do {
306                 error = rtas_call(rtas_token("get-time-of-day"), 0, 8, ret);
307                 if (error == RTAS_CLOCK_BUSY || rtas_is_extended_busy(error)) {
308                         if (in_interrupt() && printk_ratelimit()) {
309                                 printk(KERN_WARNING "error: reading clock would delay interrupt\n");
310                                 return; /* delay not allowed */
311                         }
312                         wait_time = rtas_extended_busy_delay_time(error);
313                         msleep_interruptible(wait_time);
314                         error = RTAS_CLOCK_BUSY;
315                 }
316         } while (error == RTAS_CLOCK_BUSY && (__get_tb() < max_wait_tb));
317
318         if (error != 0 && printk_ratelimit()) {
319                 printk(KERN_WARNING "error: reading the clock failed (%d)\n",
320                        error);
321                 return;
322         }
323
324         rtc_tm->tm_sec = ret[5];
325         rtc_tm->tm_min = ret[4];
326         rtc_tm->tm_hour = ret[3];
327         rtc_tm->tm_mday = ret[2];
328         rtc_tm->tm_mon = ret[1] - 1;
329         rtc_tm->tm_year = ret[0] - 1900;
330 }
331
332 int rtas_set_rtc_time(struct rtc_time *tm)
333 {
334         int error, wait_time;
335         unsigned long max_wait_tb;
336
337         max_wait_tb = __get_tb() + tb_ticks_per_usec * 1000 * MAX_RTC_WAIT;
338         do {
339                 error = rtas_call(rtas_token("set-time-of-day"), 7, 1, NULL,
340                                   tm->tm_year + 1900, tm->tm_mon + 1, 
341                                   tm->tm_mday, tm->tm_hour, tm->tm_min, 
342                                   tm->tm_sec, 0);
343                 if (error == RTAS_CLOCK_BUSY || rtas_is_extended_busy(error)) {
344                         if (in_interrupt())
345                                 return 1;       /* probably decrementer */
346                         wait_time = rtas_extended_busy_delay_time(error);
347                         msleep_interruptible(wait_time);
348                         error = RTAS_CLOCK_BUSY;
349                 }
350         } while (error == RTAS_CLOCK_BUSY && (__get_tb() < max_wait_tb));
351
352         if (error != 0 && printk_ratelimit())
353                 printk(KERN_WARNING "error: setting the clock failed (%d)\n",
354                        error); 
355
356         return 0;
357 }
358 #endif