dann frazier | 5e3fd9e | 2009-03-31 15:24:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * rtc-efi: RTC Class Driver for EFI-based systems |
| 3 | * |
| 4 | * Copyright (C) 2009 Hewlett-Packard Development Company, L.P. |
| 5 | * |
| 6 | * Author: dann frazier <dannf@hp.com> |
| 7 | * Based on efirtc.c by Stephane Eranian |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify it |
| 10 | * under the terms of the GNU General Public License as published by the |
| 11 | * Free Software Foundation; either version 2 of the License, or (at your |
| 12 | * option) any later version. |
| 13 | * |
| 14 | */ |
| 15 | |
Jingoo Han | 34650f9 | 2013-02-21 16:45:25 -0800 | [diff] [blame] | 16 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 17 | |
dann frazier | 5e3fd9e | 2009-03-31 15:24:48 -0700 | [diff] [blame] | 18 | #include <linux/kernel.h> |
| 19 | #include <linux/module.h> |
Jan Beulich | 6e85bab | 2014-08-08 14:20:09 -0700 | [diff] [blame] | 20 | #include <linux/stringify.h> |
dann frazier | 5e3fd9e | 2009-03-31 15:24:48 -0700 | [diff] [blame] | 21 | #include <linux/time.h> |
| 22 | #include <linux/platform_device.h> |
| 23 | #include <linux/rtc.h> |
| 24 | #include <linux/efi.h> |
| 25 | |
| 26 | #define EFI_ISDST (EFI_TIME_ADJUST_DAYLIGHT|EFI_TIME_IN_DAYLIGHT) |
| 27 | /* |
| 28 | * EFI Epoch is 1/1/1998 |
| 29 | */ |
| 30 | #define EFI_RTC_EPOCH 1998 |
| 31 | |
| 32 | /* |
| 33 | * returns day of the year [0-365] |
| 34 | */ |
| 35 | static inline int |
| 36 | compute_yday(efi_time_t *eft) |
| 37 | { |
| 38 | /* efi_time_t.month is in the [1-12] so, we need -1 */ |
Lee, Chun-Yi | 809d962 | 2014-06-06 14:35:48 -0700 | [diff] [blame] | 39 | return rtc_year_days(eft->day, eft->month - 1, eft->year); |
dann frazier | 5e3fd9e | 2009-03-31 15:24:48 -0700 | [diff] [blame] | 40 | } |
| 41 | /* |
| 42 | * returns day of the week [0-6] 0=Sunday |
| 43 | * |
| 44 | * Don't try to provide a year that's before 1998, please ! |
| 45 | */ |
| 46 | static int |
| 47 | compute_wday(efi_time_t *eft) |
| 48 | { |
| 49 | int y; |
| 50 | int ndays = 0; |
| 51 | |
Jan Beulich | 6e85bab | 2014-08-08 14:20:09 -0700 | [diff] [blame] | 52 | if (eft->year < EFI_RTC_EPOCH) { |
| 53 | pr_err("EFI year < " __stringify(EFI_RTC_EPOCH) ", invalid date\n"); |
dann frazier | 5e3fd9e | 2009-03-31 15:24:48 -0700 | [diff] [blame] | 54 | return -1; |
| 55 | } |
| 56 | |
| 57 | for (y = EFI_RTC_EPOCH; y < eft->year; y++) |
| 58 | ndays += 365 + (is_leap_year(y) ? 1 : 0); |
| 59 | |
| 60 | ndays += compute_yday(eft); |
| 61 | |
| 62 | /* |
| 63 | * 4=1/1/1998 was a Thursday |
| 64 | */ |
| 65 | return (ndays + 4) % 7; |
| 66 | } |
| 67 | |
| 68 | static void |
| 69 | convert_to_efi_time(struct rtc_time *wtime, efi_time_t *eft) |
| 70 | { |
| 71 | eft->year = wtime->tm_year + 1900; |
| 72 | eft->month = wtime->tm_mon + 1; |
| 73 | eft->day = wtime->tm_mday; |
| 74 | eft->hour = wtime->tm_hour; |
| 75 | eft->minute = wtime->tm_min; |
Jingoo Han | 34650f9 | 2013-02-21 16:45:25 -0800 | [diff] [blame] | 76 | eft->second = wtime->tm_sec; |
dann frazier | 5e3fd9e | 2009-03-31 15:24:48 -0700 | [diff] [blame] | 77 | eft->nanosecond = 0; |
| 78 | eft->daylight = wtime->tm_isdst ? EFI_ISDST : 0; |
| 79 | eft->timezone = EFI_UNSPECIFIED_TIMEZONE; |
| 80 | } |
| 81 | |
Jan Beulich | 6e85bab | 2014-08-08 14:20:09 -0700 | [diff] [blame] | 82 | static bool |
dann frazier | 5e3fd9e | 2009-03-31 15:24:48 -0700 | [diff] [blame] | 83 | convert_from_efi_time(efi_time_t *eft, struct rtc_time *wtime) |
| 84 | { |
| 85 | memset(wtime, 0, sizeof(*wtime)); |
Jan Beulich | 6e85bab | 2014-08-08 14:20:09 -0700 | [diff] [blame] | 86 | |
| 87 | if (eft->second >= 60) |
| 88 | return false; |
dann frazier | 5e3fd9e | 2009-03-31 15:24:48 -0700 | [diff] [blame] | 89 | wtime->tm_sec = eft->second; |
Jan Beulich | 6e85bab | 2014-08-08 14:20:09 -0700 | [diff] [blame] | 90 | |
| 91 | if (eft->minute >= 60) |
| 92 | return false; |
dann frazier | 5e3fd9e | 2009-03-31 15:24:48 -0700 | [diff] [blame] | 93 | wtime->tm_min = eft->minute; |
Jan Beulich | 6e85bab | 2014-08-08 14:20:09 -0700 | [diff] [blame] | 94 | |
| 95 | if (eft->hour >= 24) |
| 96 | return false; |
dann frazier | 5e3fd9e | 2009-03-31 15:24:48 -0700 | [diff] [blame] | 97 | wtime->tm_hour = eft->hour; |
Jan Beulich | 6e85bab | 2014-08-08 14:20:09 -0700 | [diff] [blame] | 98 | |
| 99 | if (!eft->day || eft->day > 31) |
| 100 | return false; |
dann frazier | 5e3fd9e | 2009-03-31 15:24:48 -0700 | [diff] [blame] | 101 | wtime->tm_mday = eft->day; |
Jan Beulich | 6e85bab | 2014-08-08 14:20:09 -0700 | [diff] [blame] | 102 | |
| 103 | if (!eft->month || eft->month > 12) |
| 104 | return false; |
dann frazier | 5e3fd9e | 2009-03-31 15:24:48 -0700 | [diff] [blame] | 105 | wtime->tm_mon = eft->month - 1; |
| 106 | wtime->tm_year = eft->year - 1900; |
| 107 | |
| 108 | /* day of the week [0-6], Sunday=0 */ |
| 109 | wtime->tm_wday = compute_wday(eft); |
Jan Beulich | 6e85bab | 2014-08-08 14:20:09 -0700 | [diff] [blame] | 110 | if (wtime->tm_wday < 0) |
| 111 | return false; |
dann frazier | 5e3fd9e | 2009-03-31 15:24:48 -0700 | [diff] [blame] | 112 | |
| 113 | /* day in the year [1-365]*/ |
| 114 | wtime->tm_yday = compute_yday(eft); |
| 115 | |
| 116 | |
| 117 | switch (eft->daylight & EFI_ISDST) { |
| 118 | case EFI_ISDST: |
| 119 | wtime->tm_isdst = 1; |
| 120 | break; |
| 121 | case EFI_TIME_ADJUST_DAYLIGHT: |
| 122 | wtime->tm_isdst = 0; |
| 123 | break; |
| 124 | default: |
| 125 | wtime->tm_isdst = -1; |
| 126 | } |
Jan Beulich | 6e85bab | 2014-08-08 14:20:09 -0700 | [diff] [blame] | 127 | |
| 128 | return true; |
dann frazier | 5e3fd9e | 2009-03-31 15:24:48 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | static int efi_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm) |
| 132 | { |
| 133 | efi_time_t eft; |
| 134 | efi_status_t status; |
| 135 | |
| 136 | /* |
| 137 | * As of EFI v1.10, this call always returns an unsupported status |
| 138 | */ |
| 139 | status = efi.get_wakeup_time((efi_bool_t *)&wkalrm->enabled, |
| 140 | (efi_bool_t *)&wkalrm->pending, &eft); |
| 141 | |
| 142 | if (status != EFI_SUCCESS) |
| 143 | return -EINVAL; |
| 144 | |
Jan Beulich | 6e85bab | 2014-08-08 14:20:09 -0700 | [diff] [blame] | 145 | if (!convert_from_efi_time(&eft, &wkalrm->time)) |
| 146 | return -EIO; |
dann frazier | 5e3fd9e | 2009-03-31 15:24:48 -0700 | [diff] [blame] | 147 | |
| 148 | return rtc_valid_tm(&wkalrm->time); |
| 149 | } |
| 150 | |
| 151 | static int efi_set_alarm(struct device *dev, struct rtc_wkalrm *wkalrm) |
| 152 | { |
| 153 | efi_time_t eft; |
| 154 | efi_status_t status; |
| 155 | |
| 156 | convert_to_efi_time(&wkalrm->time, &eft); |
| 157 | |
| 158 | /* |
| 159 | * XXX Fixme: |
| 160 | * As of EFI 0.92 with the firmware I have on my |
| 161 | * machine this call does not seem to work quite |
| 162 | * right |
| 163 | * |
| 164 | * As of v1.10, this call always returns an unsupported status |
| 165 | */ |
| 166 | status = efi.set_wakeup_time((efi_bool_t)wkalrm->enabled, &eft); |
| 167 | |
Jingoo Han | 34650f9 | 2013-02-21 16:45:25 -0800 | [diff] [blame] | 168 | dev_warn(dev, "write status is %d\n", (int)status); |
dann frazier | 5e3fd9e | 2009-03-31 15:24:48 -0700 | [diff] [blame] | 169 | |
| 170 | return status == EFI_SUCCESS ? 0 : -EINVAL; |
| 171 | } |
| 172 | |
| 173 | static int efi_read_time(struct device *dev, struct rtc_time *tm) |
| 174 | { |
| 175 | efi_status_t status; |
| 176 | efi_time_t eft; |
| 177 | efi_time_cap_t cap; |
| 178 | |
| 179 | status = efi.get_time(&eft, &cap); |
| 180 | |
| 181 | if (status != EFI_SUCCESS) { |
| 182 | /* should never happen */ |
Jingoo Han | 34650f9 | 2013-02-21 16:45:25 -0800 | [diff] [blame] | 183 | dev_err(dev, "can't read time\n"); |
dann frazier | 5e3fd9e | 2009-03-31 15:24:48 -0700 | [diff] [blame] | 184 | return -EINVAL; |
| 185 | } |
| 186 | |
Jan Beulich | 6e85bab | 2014-08-08 14:20:09 -0700 | [diff] [blame] | 187 | if (!convert_from_efi_time(&eft, tm)) |
| 188 | return -EIO; |
dann frazier | 5e3fd9e | 2009-03-31 15:24:48 -0700 | [diff] [blame] | 189 | |
| 190 | return rtc_valid_tm(tm); |
| 191 | } |
| 192 | |
| 193 | static int efi_set_time(struct device *dev, struct rtc_time *tm) |
| 194 | { |
| 195 | efi_status_t status; |
| 196 | efi_time_t eft; |
| 197 | |
| 198 | convert_to_efi_time(tm, &eft); |
| 199 | |
| 200 | status = efi.set_time(&eft); |
| 201 | |
| 202 | return status == EFI_SUCCESS ? 0 : -EINVAL; |
| 203 | } |
| 204 | |
| 205 | static const struct rtc_class_ops efi_rtc_ops = { |
| 206 | .read_time = efi_read_time, |
| 207 | .set_time = efi_set_time, |
| 208 | .read_alarm = efi_read_alarm, |
| 209 | .set_alarm = efi_set_alarm, |
| 210 | }; |
| 211 | |
| 212 | static int __init efi_rtc_probe(struct platform_device *dev) |
| 213 | { |
| 214 | struct rtc_device *rtc; |
| 215 | |
Jingoo Han | 6ae15b0 | 2013-04-29 16:19:36 -0700 | [diff] [blame] | 216 | rtc = devm_rtc_device_register(&dev->dev, "rtc-efi", &efi_rtc_ops, |
dann frazier | 5e3fd9e | 2009-03-31 15:24:48 -0700 | [diff] [blame] | 217 | THIS_MODULE); |
| 218 | if (IS_ERR(rtc)) |
| 219 | return PTR_ERR(rtc); |
| 220 | |
| 221 | platform_set_drvdata(dev, rtc); |
| 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
dann frazier | 5e3fd9e | 2009-03-31 15:24:48 -0700 | [diff] [blame] | 226 | static struct platform_driver efi_rtc_driver = { |
| 227 | .driver = { |
| 228 | .name = "rtc-efi", |
| 229 | .owner = THIS_MODULE, |
| 230 | }, |
dann frazier | 5e3fd9e | 2009-03-31 15:24:48 -0700 | [diff] [blame] | 231 | }; |
| 232 | |
Jingoo Han | 52c6ecb | 2013-04-29 16:18:40 -0700 | [diff] [blame] | 233 | module_platform_driver_probe(efi_rtc_driver, efi_rtc_probe); |
dann frazier | 5e3fd9e | 2009-03-31 15:24:48 -0700 | [diff] [blame] | 234 | |
Pali Rohár | 451ff6d | 2014-09-25 16:05:22 -0700 | [diff] [blame] | 235 | MODULE_ALIAS("platform:rtc-efi"); |
dann frazier | 5e3fd9e | 2009-03-31 15:24:48 -0700 | [diff] [blame] | 236 | MODULE_AUTHOR("dann frazier <dannf@hp.com>"); |
| 237 | MODULE_LICENSE("GPL"); |
| 238 | MODULE_DESCRIPTION("EFI RTC driver"); |