Merge branches 'release', 'asus', 'sony-laptop' and 'thinkpad' into release
[pandora-kernel.git] / drivers / char / ip27-rtc.c
1 /*
2  *      Driver for the SGS-Thomson M48T35 Timekeeper RAM chip
3  *
4  *      Real Time Clock interface for Linux
5  *
6  *      TODO: Implement periodic interrupts.
7  *
8  *      Copyright (C) 2000 Silicon Graphics, Inc.
9  *      Written by Ulf Carlsson (ulfc@engr.sgi.com)
10  *
11  *      Based on code written by Paul Gortmaker.
12  *
13  *      This driver allows use of the real time clock (built into
14  *      nearly all computers) from user space. It exports the /dev/rtc
15  *      interface supporting various ioctl() and also the /proc/rtc
16  *      pseudo-file for status information.
17  *
18  *      This program is free software; you can redistribute it and/or
19  *      modify it under the terms of the GNU General Public License
20  *      as published by the Free Software Foundation; either version
21  *      2 of the License, or (at your option) any later version.
22  *
23  */
24
25 #define RTC_VERSION             "1.09b"
26
27 #include <linux/bcd.h>
28 #include <linux/module.h>
29 #include <linux/kernel.h>
30 #include <linux/types.h>
31 #include <linux/miscdevice.h>
32 #include <linux/ioport.h>
33 #include <linux/fcntl.h>
34 #include <linux/rtc.h>
35 #include <linux/init.h>
36 #include <linux/poll.h>
37 #include <linux/proc_fs.h>
38
39 #include <asm/m48t35.h>
40 #include <asm/sn/ioc3.h>
41 #include <asm/io.h>
42 #include <asm/uaccess.h>
43 #include <asm/system.h>
44 #include <asm/sn/klconfig.h>
45 #include <asm/sn/sn0/ip27.h>
46 #include <asm/sn/sn0/hub.h>
47 #include <asm/sn/sn_private.h>
48
49 static long rtc_ioctl(struct file *filp, unsigned int cmd,
50                         unsigned long arg);
51
52 static int rtc_read_proc(char *page, char **start, off_t off,
53                          int count, int *eof, void *data);
54
55 static void get_rtc_time(struct rtc_time *rtc_tm);
56
57 /*
58  *      Bits in rtc_status. (6 bits of room for future expansion)
59  */
60
61 #define RTC_IS_OPEN             0x01    /* means /dev/rtc is in use     */
62 #define RTC_TIMER_ON            0x02    /* missed irq timer active      */
63
64 static unsigned char rtc_status;        /* bitmapped status byte.       */
65 static unsigned long rtc_freq;  /* Current periodic IRQ rate    */
66 static struct m48t35_rtc *rtc;
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 = 1970;      /* 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 static long rtc_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
79 {
80
81         struct rtc_time wtime;
82
83         switch (cmd) {
84         case RTC_RD_TIME:       /* Read the time/date from RTC  */
85         {
86                 get_rtc_time(&wtime);
87                 break;
88         }
89         case RTC_SET_TIME:      /* Set the RTC */
90         {
91                 struct rtc_time rtc_tm;
92                 unsigned char mon, day, hrs, min, sec, leap_yr;
93                 unsigned int yrs;
94
95                 if (!capable(CAP_SYS_TIME))
96                         return -EACCES;
97
98                 if (copy_from_user(&rtc_tm, (struct rtc_time*)arg,
99                                    sizeof(struct rtc_time)))
100                         return -EFAULT;
101
102                 yrs = rtc_tm.tm_year + 1900;
103                 mon = rtc_tm.tm_mon + 1;   /* tm_mon starts at zero */
104                 day = rtc_tm.tm_mday;
105                 hrs = rtc_tm.tm_hour;
106                 min = rtc_tm.tm_min;
107                 sec = rtc_tm.tm_sec;
108
109                 if (yrs < 1970)
110                         return -EINVAL;
111
112                 leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
113
114                 if ((mon > 12) || (day == 0))
115                         return -EINVAL;
116
117                 if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
118                         return -EINVAL;
119
120                 if ((hrs >= 24) || (min >= 60) || (sec >= 60))
121                         return -EINVAL;
122
123                 if ((yrs -= epoch) > 255)    /* They are unsigned */
124                         return -EINVAL;
125
126                 if (yrs > 169)
127                         return -EINVAL;
128
129                 if (yrs >= 100)
130                         yrs -= 100;
131
132                 sec = BIN2BCD(sec);
133                 min = BIN2BCD(min);
134                 hrs = BIN2BCD(hrs);
135                 day = BIN2BCD(day);
136                 mon = BIN2BCD(mon);
137                 yrs = BIN2BCD(yrs);
138
139                 spin_lock_irq(&rtc_lock);
140                 rtc->control |= M48T35_RTC_SET;
141                 rtc->year = yrs;
142                 rtc->month = mon;
143                 rtc->date = day;
144                 rtc->hour = hrs;
145                 rtc->min = min;
146                 rtc->sec = sec;
147                 rtc->control &= ~M48T35_RTC_SET;
148                 spin_unlock_irq(&rtc_lock);
149
150                 return 0;
151         }
152         default:
153                 return -EINVAL;
154         }
155         return copy_to_user((void *)arg, &wtime, sizeof wtime) ? -EFAULT : 0;
156 }
157
158 /*
159  *      We enforce only one user at a time here with the open/close.
160  *      Also clear the previous interrupt data on an open, and clean
161  *      up things on a close.
162  */
163
164 static int rtc_open(struct inode *inode, struct file *file)
165 {
166         spin_lock_irq(&rtc_lock);
167
168         if (rtc_status & RTC_IS_OPEN) {
169                 spin_unlock_irq(&rtc_lock);
170                 return -EBUSY;
171         }
172
173         rtc_status |= RTC_IS_OPEN;
174         spin_unlock_irq(&rtc_lock);
175
176         return 0;
177 }
178
179 static int rtc_release(struct inode *inode, struct file *file)
180 {
181         /*
182          * Turn off all interrupts once the device is no longer
183          * in use, and clear the data.
184          */
185
186         spin_lock_irq(&rtc_lock);
187         rtc_status &= ~RTC_IS_OPEN;
188         spin_unlock_irq(&rtc_lock);
189
190         return 0;
191 }
192
193 /*
194  *      The various file operations we support.
195  */
196
197 static const struct file_operations rtc_fops = {
198         .owner          = THIS_MODULE,
199         .unlocked_ioctl = rtc_ioctl,
200         .open           = rtc_open,
201         .release        = rtc_release,
202 };
203
204 static struct miscdevice rtc_dev=
205 {
206         RTC_MINOR,
207         "rtc",
208         &rtc_fops
209 };
210
211 static int __init rtc_init(void)
212 {
213         rtc = (struct m48t35_rtc *)
214         (KL_CONFIG_CH_CONS_INFO(master_nasid)->memory_base + IOC3_BYTEBUS_DEV0);
215
216         printk(KERN_INFO "Real Time Clock Driver v%s\n", RTC_VERSION);
217         if (misc_register(&rtc_dev)) {
218                 printk(KERN_ERR "rtc: cannot register misc device.\n");
219                 return -ENODEV;
220         }
221         if (!create_proc_read_entry("driver/rtc", 0, NULL, rtc_read_proc, NULL)) {
222                 printk(KERN_ERR "rtc: cannot create /proc/rtc.\n");
223                 misc_deregister(&rtc_dev);
224                 return -ENOENT;
225         }
226
227         rtc_freq = 1024;
228
229         return 0;
230 }
231
232 static void __exit rtc_exit (void)
233 {
234         /* interrupts and timer disabled at this point by rtc_release */
235
236         remove_proc_entry ("rtc", NULL);
237         misc_deregister(&rtc_dev);
238 }
239
240 module_init(rtc_init);
241 module_exit(rtc_exit);
242
243 /*
244  *      Info exported via "/proc/rtc".
245  */
246
247 static int rtc_get_status(char *buf)
248 {
249         char *p;
250         struct rtc_time tm;
251
252         /*
253          * Just emulate the standard /proc/rtc
254          */
255
256         p = buf;
257
258         get_rtc_time(&tm);
259
260         /*
261          * There is no way to tell if the luser has the RTC set for local
262          * time or for Universal Standard Time (GMT). Probably local though.
263          */
264         p += sprintf(p,
265                      "rtc_time\t: %02d:%02d:%02d\n"
266                      "rtc_date\t: %04d-%02d-%02d\n"
267                      "rtc_epoch\t: %04lu\n"
268                      "24hr\t\t: yes\n",
269                      tm.tm_hour, tm.tm_min, tm.tm_sec,
270                      tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, epoch);
271
272         return  p - buf;
273 }
274
275 static int rtc_read_proc(char *page, char **start, off_t off,
276                                  int count, int *eof, void *data)
277 {
278         int len = rtc_get_status(page);
279         if (len <= off+count) *eof = 1;
280         *start = page + off;
281         len -= off;
282         if (len>count) len = count;
283         if (len<0) len = 0;
284         return len;
285 }
286
287 static void get_rtc_time(struct rtc_time *rtc_tm)
288 {
289         /*
290          * Do we need to wait for the last update to finish?
291          */
292
293         /*
294          * Only the values that we read from the RTC are set. We leave
295          * tm_wday, tm_yday and tm_isdst untouched. Even though the
296          * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
297          * by the RTC when initially set to a non-zero value.
298          */
299         spin_lock_irq(&rtc_lock);
300         rtc->control |= M48T35_RTC_READ;
301         rtc_tm->tm_sec = rtc->sec;
302         rtc_tm->tm_min = rtc->min;
303         rtc_tm->tm_hour = rtc->hour;
304         rtc_tm->tm_mday = rtc->date;
305         rtc_tm->tm_mon = rtc->month;
306         rtc_tm->tm_year = rtc->year;
307         rtc->control &= ~M48T35_RTC_READ;
308         spin_unlock_irq(&rtc_lock);
309
310         rtc_tm->tm_sec = BCD2BIN(rtc_tm->tm_sec);
311         rtc_tm->tm_min = BCD2BIN(rtc_tm->tm_min);
312         rtc_tm->tm_hour = BCD2BIN(rtc_tm->tm_hour);
313         rtc_tm->tm_mday = BCD2BIN(rtc_tm->tm_mday);
314         rtc_tm->tm_mon = BCD2BIN(rtc_tm->tm_mon);
315         rtc_tm->tm_year = BCD2BIN(rtc_tm->tm_year);
316
317         /*
318          * Account for differences between how the RTC uses the values
319          * and how they are defined in a struct rtc_time;
320          */
321         if ((rtc_tm->tm_year += (epoch - 1900)) <= 69)
322                 rtc_tm->tm_year += 100;
323
324         rtc_tm->tm_mon--;
325 }