2 * EFI Time Services Driver for Linux
4 * Copyright (C) 1999 Hewlett-Packard Co
5 * Copyright (C) 1999 Stephane Eranian <eranian@hpl.hp.com>
7 * Based on skeleton from the drivers/char/rtc.c driver by P. Gortmaker
9 * This code provides an 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
15 * Please note that we have kept the API as close as possible to the
16 * legacy RTC. The standard /sbin/hwclock program should work normally
17 * when used to get/set the time.
20 * - Locking is required for safe execution of EFI calls with regards
21 * to interrupts and SMP.
23 * TODO (December 1999):
24 * - provide the API to set/get the WakeUp Alarm (different from the
27 * - Add module support
31 #include <linux/smp_lock.h>
32 #include <linux/types.h>
33 #include <linux/errno.h>
34 #include <linux/miscdevice.h>
35 #include <linux/module.h>
36 #include <linux/init.h>
37 #include <linux/rtc.h>
38 #include <linux/proc_fs.h>
39 #include <linux/efi.h>
41 #include <asm/uaccess.h>
42 #include <asm/system.h>
44 #define EFI_RTC_VERSION "0.4"
46 #define EFI_ISDST (EFI_TIME_ADJUST_DAYLIGHT|EFI_TIME_IN_DAYLIGHT)
48 * EFI Epoch is 1/1/1998
50 #define EFI_RTC_EPOCH 1998
52 static DEFINE_SPINLOCK(efi_rtc_lock);
54 static int efi_rtc_ioctl(struct inode *inode, struct file *file,
55 unsigned int cmd, unsigned long arg);
57 #define is_leap(year) \
58 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
60 static const unsigned short int __mon_yday[2][13] =
63 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
65 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
69 * returns day of the year [0-365]
72 compute_yday(efi_time_t *eft)
74 /* efi_time_t.month is in the [1-12] so, we need -1 */
75 return __mon_yday[is_leap(eft->year)][eft->month-1]+ eft->day -1;
78 * returns day of the week [0-6] 0=Sunday
80 * Don't try to provide a year that's before 1998, please !
83 compute_wday(efi_time_t *eft)
88 if ( eft->year < 1998 ) {
89 printk(KERN_ERR "efirtc: EFI year < 1998, invalid date\n");
93 for(y=EFI_RTC_EPOCH; y < eft->year; y++ ) {
94 ndays += 365 + (is_leap(y) ? 1 : 0);
96 ndays += compute_yday(eft);
99 * 4=1/1/1998 was a Thursday
101 return (ndays + 4) % 7;
105 convert_to_efi_time(struct rtc_time *wtime, efi_time_t *eft)
108 eft->year = wtime->tm_year + 1900;
109 eft->month = wtime->tm_mon + 1;
110 eft->day = wtime->tm_mday;
111 eft->hour = wtime->tm_hour;
112 eft->minute = wtime->tm_min;
113 eft->second = wtime->tm_sec;
115 eft->daylight = wtime->tm_isdst ? EFI_ISDST: 0;
116 eft->timezone = EFI_UNSPECIFIED_TIMEZONE;
120 convert_from_efi_time(efi_time_t *eft, struct rtc_time *wtime)
122 memset(wtime, 0, sizeof(*wtime));
123 wtime->tm_sec = eft->second;
124 wtime->tm_min = eft->minute;
125 wtime->tm_hour = eft->hour;
126 wtime->tm_mday = eft->day;
127 wtime->tm_mon = eft->month - 1;
128 wtime->tm_year = eft->year - 1900;
130 /* day of the week [0-6], Sunday=0 */
131 wtime->tm_wday = compute_wday(eft);
133 /* day in the year [1-365]*/
134 wtime->tm_yday = compute_yday(eft);
137 switch (eft->daylight & EFI_ISDST) {
141 case EFI_TIME_ADJUST_DAYLIGHT:
145 wtime->tm_isdst = -1;
150 efi_rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
158 struct rtc_time wtime;
159 struct rtc_wkalrm __user *ewp;
160 unsigned char enabled, pending;
179 spin_lock_irqsave(&efi_rtc_lock, flags);
181 status = efi.get_time(&eft, &cap);
183 spin_unlock_irqrestore(&efi_rtc_lock,flags);
185 if (status != EFI_SUCCESS) {
186 /* should never happen */
187 printk(KERN_ERR "efitime: can't read time\n");
191 convert_from_efi_time(&eft, &wtime);
193 return copy_to_user((void __user *)arg, &wtime,
194 sizeof (struct rtc_time)) ? - EFAULT : 0;
198 if (!capable(CAP_SYS_TIME)) return -EACCES;
200 if (copy_from_user(&wtime, (struct rtc_time __user *)arg,
201 sizeof(struct rtc_time)) )
204 convert_to_efi_time(&wtime, &eft);
206 spin_lock_irqsave(&efi_rtc_lock, flags);
208 status = efi.set_time(&eft);
210 spin_unlock_irqrestore(&efi_rtc_lock,flags);
212 return status == EFI_SUCCESS ? 0 : -EINVAL;
216 if (!capable(CAP_SYS_TIME)) return -EACCES;
218 ewp = (struct rtc_wkalrm __user *)arg;
220 if ( get_user(enabled, &ewp->enabled)
221 || copy_from_user(&wtime, &ewp->time, sizeof(struct rtc_time)) )
224 convert_to_efi_time(&wtime, &eft);
226 spin_lock_irqsave(&efi_rtc_lock, flags);
229 * As of EFI 0.92 with the firmware I have on my
230 * machine this call does not seem to work quite
233 status = efi.set_wakeup_time((efi_bool_t)enabled, &eft);
235 spin_unlock_irqrestore(&efi_rtc_lock,flags);
237 return status == EFI_SUCCESS ? 0 : -EINVAL;
241 spin_lock_irqsave(&efi_rtc_lock, flags);
243 status = efi.get_wakeup_time((efi_bool_t *)&enabled, (efi_bool_t *)&pending, &eft);
245 spin_unlock_irqrestore(&efi_rtc_lock,flags);
247 if (status != EFI_SUCCESS) return -EINVAL;
249 ewp = (struct rtc_wkalrm __user *)arg;
251 if ( put_user(enabled, &ewp->enabled)
252 || put_user(pending, &ewp->pending)) return -EFAULT;
254 convert_from_efi_time(&eft, &wtime);
256 return copy_to_user(&ewp->time, &wtime,
257 sizeof(struct rtc_time)) ? -EFAULT : 0;
263 * We enforce only one user at a time here with the open/close.
264 * Also clear the previous interrupt data on an open, and clean
265 * up things on a close.
269 efi_rtc_open(struct inode *inode, struct file *file)
272 * nothing special to do here
273 * We do accept multiple open files at the same time as we
274 * synchronize on the per call operation.
281 efi_rtc_close(struct inode *inode, struct file *file)
287 * The various file operations we support.
290 static const struct file_operations efi_rtc_fops = {
291 .owner = THIS_MODULE,
292 .ioctl = efi_rtc_ioctl,
293 .open = efi_rtc_open,
294 .release = efi_rtc_close,
297 static struct miscdevice efi_rtc_dev=
305 * We export RAW EFI information to /proc/driver/efirtc
308 efi_rtc_get_status(char *buf)
313 efi_bool_t enabled, pending;
316 memset(&eft, 0, sizeof(eft));
317 memset(&alm, 0, sizeof(alm));
318 memset(&cap, 0, sizeof(cap));
320 spin_lock_irqsave(&efi_rtc_lock, flags);
322 efi.get_time(&eft, &cap);
323 efi.get_wakeup_time(&enabled, &pending, &alm);
325 spin_unlock_irqrestore(&efi_rtc_lock,flags);
328 "Time : %u:%u:%u.%09u\n"
331 eft.hour, eft.minute, eft.second, eft.nanosecond,
332 eft.year, eft.month, eft.day,
335 if (eft.timezone == EFI_UNSPECIFIED_TIMEZONE)
336 p += sprintf(p, "Timezone : unspecified\n");
338 /* XXX fixme: convert to string? */
339 p += sprintf(p, "Timezone : %u\n", eft.timezone);
343 "Alarm Time : %u:%u:%u.%09u\n"
344 "Alarm Date : %u-%u-%u\n"
345 "Alarm Daylight : %u\n"
348 alm.hour, alm.minute, alm.second, alm.nanosecond,
349 alm.year, alm.month, alm.day,
351 enabled == 1 ? "yes" : "no",
352 pending == 1 ? "yes" : "no");
354 if (eft.timezone == EFI_UNSPECIFIED_TIMEZONE)
355 p += sprintf(p, "Timezone : unspecified\n");
357 /* XXX fixme: convert to string? */
358 p += sprintf(p, "Timezone : %u\n", alm.timezone);
361 * now prints the capabilities
367 cap.resolution, cap.accuracy, cap.sets_to_zero);
373 efi_rtc_read_proc(char *page, char **start, off_t off,
374 int count, int *eof, void *data)
376 int len = efi_rtc_get_status(page);
377 if (len <= off+count) *eof = 1;
380 if (len>count) len = count;
389 struct proc_dir_entry *dir;
391 printk(KERN_INFO "EFI Time Services Driver v%s\n", EFI_RTC_VERSION);
393 ret = misc_register(&efi_rtc_dev);
395 printk(KERN_ERR "efirtc: can't misc_register on minor=%d\n",
400 dir = create_proc_read_entry ("driver/efirtc", 0, NULL,
401 efi_rtc_read_proc, NULL);
403 printk(KERN_ERR "efirtc: can't create /proc/driver/efirtc.\n");
404 misc_deregister(&efi_rtc_dev);
416 module_init(efi_rtc_init);
417 module_exit(efi_rtc_exit);
419 MODULE_LICENSE("GPL");