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