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