xref: /openbmc/linux/drivers/rtc/rtc-efi.c (revision 3932b9ca)
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 
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/stringify.h>
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 */
39 	return rtc_year_days(eft->day, eft->month - 1, eft->year);
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 
52 	if (eft->year < EFI_RTC_EPOCH) {
53 		pr_err("EFI year < " __stringify(EFI_RTC_EPOCH) ", invalid date\n");
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;
76 	eft->second	= wtime->tm_sec;
77 	eft->nanosecond = 0;
78 	eft->daylight	= wtime->tm_isdst ? EFI_ISDST : 0;
79 	eft->timezone	= EFI_UNSPECIFIED_TIMEZONE;
80 }
81 
82 static bool
83 convert_from_efi_time(efi_time_t *eft, struct rtc_time *wtime)
84 {
85 	memset(wtime, 0, sizeof(*wtime));
86 
87 	if (eft->second >= 60)
88 		return false;
89 	wtime->tm_sec  = eft->second;
90 
91 	if (eft->minute >= 60)
92 		return false;
93 	wtime->tm_min  = eft->minute;
94 
95 	if (eft->hour >= 24)
96 		return false;
97 	wtime->tm_hour = eft->hour;
98 
99 	if (!eft->day || eft->day > 31)
100 		return false;
101 	wtime->tm_mday = eft->day;
102 
103 	if (!eft->month || eft->month > 12)
104 		return false;
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);
110 	if (wtime->tm_wday < 0)
111 		return false;
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 	}
127 
128 	return true;
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 
145 	if (!convert_from_efi_time(&eft, &wkalrm->time))
146 		return -EIO;
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 
168 	dev_warn(dev, "write status is %d\n", (int)status);
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 */
183 		dev_err(dev, "can't read time\n");
184 		return -EINVAL;
185 	}
186 
187 	if (!convert_from_efi_time(&eft, tm))
188 		return -EIO;
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 
216 	rtc = devm_rtc_device_register(&dev->dev, "rtc-efi", &efi_rtc_ops,
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 
226 static struct platform_driver efi_rtc_driver = {
227 	.driver = {
228 		.name = "rtc-efi",
229 		.owner = THIS_MODULE,
230 	},
231 };
232 
233 module_platform_driver_probe(efi_rtc_driver, efi_rtc_probe);
234 
235 MODULE_ALIAS("platform:rtc-efi");
236 MODULE_AUTHOR("dann frazier <dannf@hp.com>");
237 MODULE_LICENSE("GPL");
238 MODULE_DESCRIPTION("EFI RTC driver");
239