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