OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [char/] [efirtc.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
2
 * EFI Time Services Driver for Linux
3
 *
4
 * Copyright (C) 1999 Hewlett-Packard Co
5
 * Copyright (C) 1999 Stephane Eranian <eranian@hpl.hp.com>
6
 *
7
 * Based on skeleton from the drivers/char/rtc.c driver by P. Gortmaker
8
 *
9
 * This code provides a architected & portable interface to the real time
10
 * clock by using EFI instead of direct bit fiddling. The functionalities are
11
 * quite different from the rtc.c driver. The only way to talk to the device
12
 * is by using ioctl(). There is a /proc interface which provides the raw
13
 * information.
14
 *
15
 * Please note that we have kept the API as close as possible from the
16
 * legacy RTC. The standard /sbin/hwclock program should work normally
17
 * when used to get/set the time.
18
 *
19
 * NOTES:
20
 *      - Locking is required for safe execution of EFI calls with regards
21
 *        to interrrupts and SMP.
22
 *
23
 * TODO (December 1999):
24
 *      - provide the API to set/get the WakeUp Alarm (different from the
25
 *        rtc.c alarm).
26
 *      - SMP testing
27
 *      - Add module support
28
 */
29
 
30
 
31
#include <linux/types.h>
32
#include <linux/errno.h>
33
#include <linux/miscdevice.h>
34
#include <linux/module.h>
35
#include <linux/init.h>
36
#include <linux/rtc.h>
37
#include <linux/proc_fs.h>
38
#include <linux/efi.h>
39
 
40
#include <asm/uaccess.h>
41
#include <asm/system.h>
42
 
43
#define EFI_RTC_VERSION         "0.4"
44
 
45
#define EFI_ISDST (EFI_TIME_ADJUST_DAYLIGHT|EFI_TIME_IN_DAYLIGHT)
46
/*
47
 * EFI Epoch is 1/1/1998
48
 */
49
#define EFI_RTC_EPOCH           1998
50
 
51
static spinlock_t efi_rtc_lock = SPIN_LOCK_UNLOCKED;
52
 
53
static int efi_rtc_ioctl(struct inode *inode, struct file *file,
54
                     unsigned int cmd, unsigned long arg);
55
 
56
#define is_leap(year) \
57
          ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
58
 
59
static const unsigned short int __mon_yday[2][13] =
60
{
61
        /* Normal years.  */
62
        { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
63
        /* Leap years.  */
64
        { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
65
};
66
 
67
/*
68
 * returns day of the year [0-365]
69
 */
70
static inline int
71
compute_yday(efi_time_t *eft)
72
{
73
        /* efi_time_t.month is in the [1-12] so, we need -1 */
74
        return  __mon_yday[is_leap(eft->year)][eft->month-1]+ eft->day -1;
75
}
76
/*
77
 * returns day of the week [0-6] 0=Sunday
78
 *
79
 * Don't try to provide a year that's before 1998, please !
80
 */
81
static int
82
compute_wday(efi_time_t *eft)
83
{
84
        int y;
85
        int ndays = 0;
86
 
87
        if ( eft->year < 1998 ) {
88
                printk(KERN_ERR "efirtc: EFI year < 1998, invalid date\n");
89
                return -1;
90
        }
91
 
92
        for(y=EFI_RTC_EPOCH; y < eft->year; y++ ) {
93
                ndays += 365 + (is_leap(y) ? 1 : 0);
94
        }
95
        ndays += compute_yday(eft);
96
 
97
        /*
98
         * 4=1/1/1998 was a Thursday
99
         */
100
        return (ndays + 4) % 7;
101
}
102
 
103
static void
104
convert_to_efi_time(struct rtc_time *wtime, efi_time_t *eft)
105
{
106
 
107
        eft->year       = wtime->tm_year + 1900;
108
        eft->month      = wtime->tm_mon + 1;
109
        eft->day        = wtime->tm_mday;
110
        eft->hour       = wtime->tm_hour;
111
        eft->minute     = wtime->tm_min;
112
        eft->second     = wtime->tm_sec;
113
        eft->nanosecond = 0;
114
        eft->daylight   = wtime->tm_isdst ? EFI_ISDST: 0;
115
        eft->timezone   = EFI_UNSPECIFIED_TIMEZONE;
116
}
117
 
118
static void
119
convert_from_efi_time(efi_time_t *eft, struct rtc_time *wtime)
120
{
121
        memset(wtime, 0, sizeof(struct rtc_time));
122
        wtime->tm_sec  = eft->second;
123
        wtime->tm_min  = eft->minute;
124
        wtime->tm_hour = eft->hour;
125
        wtime->tm_mday = eft->day;
126
        wtime->tm_mon  = eft->month - 1;
127
        wtime->tm_year = eft->year - 1900;
128
 
129
        /* day of the week [0-6], Sunday=0 */
130
        wtime->tm_wday = compute_wday(eft);
131
 
132
        /* day in the year [1-365]*/
133
        wtime->tm_yday = compute_yday(eft);
134
 
135
 
136
        switch (eft->daylight & EFI_ISDST) {
137
                case EFI_ISDST:
138
                        wtime->tm_isdst = 1;
139
                        break;
140
                case EFI_TIME_ADJUST_DAYLIGHT:
141
                        wtime->tm_isdst = 0;
142
                        break;
143
                default:
144
                        wtime->tm_isdst = -1;
145
        }
146
}
147
 
148
static int
149
efi_rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
150
                     unsigned long arg)
151
{
152
 
153
        efi_status_t    status;
154
        unsigned long   flags;
155
        efi_time_t      eft;
156
        efi_time_cap_t  cap;
157
        struct rtc_time wtime;
158
        struct rtc_wkalrm *ewp;
159
        unsigned char   enabled, pending;
160
 
161
        switch (cmd) {
162
                case RTC_UIE_ON:
163
                case RTC_UIE_OFF:
164
                case RTC_PIE_ON:
165
                case RTC_PIE_OFF:
166
                case RTC_AIE_ON:
167
                case RTC_AIE_OFF:
168
                case RTC_ALM_SET:
169
                case RTC_ALM_READ:
170
                case RTC_IRQP_READ:
171
                case RTC_IRQP_SET:
172
                case RTC_EPOCH_READ:
173
                case RTC_EPOCH_SET:
174
                        return -EINVAL;
175
 
176
                case RTC_RD_TIME:
177
 
178
                        spin_lock_irqsave(&efi_rtc_lock, flags);
179
 
180
                        status = efi.get_time(&eft, &cap);
181
 
182
                        spin_unlock_irqrestore(&efi_rtc_lock,flags);
183
 
184
                        if (status != EFI_SUCCESS) {
185
                                /* should never happen */
186
                                printk(KERN_ERR "efitime: can't read time\n");
187
                                return -EINVAL;
188
                        }
189
 
190
                        convert_from_efi_time(&eft, &wtime);
191
 
192
                        return copy_to_user((void *)arg, &wtime, sizeof (struct rtc_time)) ? - EFAULT : 0;
193
 
194
                case RTC_SET_TIME:
195
 
196
                        if (!capable(CAP_SYS_TIME)) return -EACCES;
197
 
198
                        if (copy_from_user(&wtime, (struct rtc_time *)arg, sizeof(struct rtc_time)) )
199
                                return -EFAULT;
200
 
201
                        convert_to_efi_time(&wtime, &eft);
202
 
203
                        spin_lock_irqsave(&efi_rtc_lock, flags);
204
 
205
                        status = efi.set_time(&eft);
206
 
207
                        spin_unlock_irqrestore(&efi_rtc_lock,flags);
208
 
209
                        return status == EFI_SUCCESS ? 0 : -EINVAL;
210
 
211
                case RTC_WKALM_SET:
212
 
213
                        if (!capable(CAP_SYS_TIME)) return -EACCES;
214
 
215
                        ewp = (struct rtc_wkalrm *)arg;
216
 
217
                        if (  get_user(enabled, &ewp->enabled)
218
                           || copy_from_user(&wtime, &ewp->time, sizeof(struct rtc_time)) )
219
                                return -EFAULT;
220
 
221
                        convert_to_efi_time(&wtime, &eft);
222
 
223
                        spin_lock_irqsave(&efi_rtc_lock, flags);
224
                        /*
225
                         * XXX Fixme:
226
                         * As of EFI 0.92 with the firmware I have on my
227
                         * machine this call does not seem to work quite
228
                         * right
229
                         */
230
                        status = efi.set_wakeup_time((efi_bool_t)enabled, &eft);
231
 
232
                        spin_unlock_irqrestore(&efi_rtc_lock,flags);
233
 
234
                        return status == EFI_SUCCESS ? 0 : -EINVAL;
235
 
236
                case RTC_WKALM_RD:
237
 
238
                        spin_lock_irqsave(&efi_rtc_lock, flags);
239
 
240
                        status = efi.get_wakeup_time((efi_bool_t *)&enabled, (efi_bool_t *)&pending, &eft);
241
 
242
                        spin_unlock_irqrestore(&efi_rtc_lock,flags);
243
 
244
                        if (status != EFI_SUCCESS) return -EINVAL;
245
 
246
                        ewp = (struct rtc_wkalrm *)arg;
247
 
248
                        if (  put_user(enabled, &ewp->enabled)
249
                           || put_user(pending, &ewp->pending)) return -EFAULT;
250
 
251
                        convert_from_efi_time(&eft, &wtime);
252
 
253
                        return copy_to_user((void *)&ewp->time, &wtime, sizeof(struct rtc_time)) ? -EFAULT : 0;
254
        }
255
        return -EINVAL;
256
}
257
 
258
/*
259
 *      We enforce only one user at a time here with the open/close.
260
 *      Also clear the previous interrupt data on an open, and clean
261
 *      up things on a close.
262
 */
263
 
264
static int
265
efi_rtc_open(struct inode *inode, struct file *file)
266
{
267
        /*
268
         * nothing special to do here
269
         * We do accept multiple open files at the same time as we
270
         * synchronize on the per call operation.
271
         */
272
        return 0;
273
}
274
 
275
static int
276
efi_rtc_close(struct inode *inode, struct file *file)
277
{
278
        return 0;
279
}
280
 
281
/*
282
 *      The various file operations we support.
283
 */
284
 
285
static struct file_operations efi_rtc_fops = {
286
        owner:          THIS_MODULE,
287
        ioctl:          efi_rtc_ioctl,
288
        open:           efi_rtc_open,
289
        release:        efi_rtc_close,
290
};
291
 
292
static struct miscdevice efi_rtc_dev=
293
{
294
        EFI_RTC_MINOR,
295
        "efirtc",
296
        &efi_rtc_fops
297
};
298
 
299
/*
300
 *      We export RAW EFI information to /proc/efirtc
301
 */
302
static int
303
efi_rtc_get_status(char *buf)
304
{
305
        efi_time_t      eft, alm;
306
        efi_time_cap_t  cap;
307
        char            *p = buf;
308
        efi_bool_t      enabled, pending;
309
        unsigned long   flags;
310
 
311
        spin_lock_irqsave(&efi_rtc_lock, flags);
312
 
313
        efi.get_time(&eft, &cap);
314
        efi.get_wakeup_time(&enabled, &pending, &alm);
315
 
316
        spin_unlock_irqrestore(&efi_rtc_lock,flags);
317
 
318
        p += sprintf(p,
319
                     "Time           : %u:%u:%u.%09u\n"
320
                     "Date           : %u-%u-%u\n"
321
                     "Daylight       : %u\n",
322
                     eft.hour, eft.minute, eft.second, eft.nanosecond,
323
                     eft.year, eft.month, eft.day,
324
                     eft.daylight);
325
 
326
        if (eft.timezone == EFI_UNSPECIFIED_TIMEZONE)
327
                p += sprintf(p, "Timezone       : unspecified\n");
328
        else
329
                /* XXX fixme: convert to string? */
330
                p += sprintf(p, "Timezone       : %u\n", eft.timezone);
331
 
332
 
333
        p += sprintf(p,
334
                     "Alarm Time     : %u:%u:%u.%09u\n"
335
                     "Alarm Date     : %u-%u-%u\n"
336
                     "Alarm Daylight : %u\n"
337
                     "Enabled        : %s\n"
338
                     "Pending        : %s\n",
339
                     alm.hour, alm.minute, alm.second, alm.nanosecond,
340
                     alm.year, alm.month, alm.day,
341
                     alm.daylight,
342
                     enabled == 1 ? "yes" : "no",
343
                     pending == 1 ? "yes" : "no");
344
 
345
        if (eft.timezone == EFI_UNSPECIFIED_TIMEZONE)
346
                p += sprintf(p, "Timezone       : unspecified\n");
347
        else
348
                /* XXX fixme: convert to string? */
349
                p += sprintf(p, "Timezone       : %u\n", alm.timezone);
350
 
351
        /*
352
         * now prints the capabilities
353
         */
354
        p += sprintf(p,
355
                     "Resolution     : %u\n"
356
                     "Accuracy       : %u\n"
357
                     "SetstoZero     : %u\n",
358
                      cap.resolution, cap.accuracy, cap.sets_to_zero);
359
 
360
        return  p - buf;
361
}
362
 
363
static int
364
efi_rtc_read_proc(char *page, char **start, off_t off,
365
                                 int count, int *eof, void *data)
366
{
367
        int len = efi_rtc_get_status(page);
368
        if (len <= off+count) *eof = 1;
369
        *start = page + off;
370
        len -= off;
371
        if (len>count) len = count;
372
        if (len<0) len = 0;
373
        return len;
374
}
375
 
376
static int __init
377
efi_rtc_init(void)
378
{
379
        int ret;
380
        struct proc_dir_entry *dir;
381
 
382
        printk(KERN_INFO "EFI Time Services Driver v%s\n", EFI_RTC_VERSION);
383
 
384
        ret = misc_register(&efi_rtc_dev);
385
        if (ret) {
386
                printk(KERN_ERR "efirtc: can't misc_register on minor=%d\n",
387
                                EFI_RTC_MINOR);
388
                return ret;
389
        }
390
 
391
        dir = create_proc_read_entry ("driver/efirtc", 0, NULL,
392
                                      efi_rtc_read_proc, NULL);
393
        if (dir == NULL) {
394
                printk(KERN_ERR "efirtc: can't create /proc/driver/efirtc.\n");
395
                misc_deregister(&efi_rtc_dev);
396
                return -1;
397
        }
398
        return 0;
399
}
400
 
401
static void __exit
402
efi_rtc_exit(void)
403
{
404
        /* not yet used */
405
}
406
 
407
module_init(efi_rtc_init);
408
module_exit(efi_rtc_exit);
409
 
410
MODULE_LICENSE("GPL");

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.