xref: /linux/drivers/rtc/rtc-opal.c (revision 0ec7769a98b00866e37740328d65cba6594d178d)
1 /*
2  * IBM OPAL RTC driver
3  * Copyright (C) 2014 IBM
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.
17  */
18 
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 
21 #define DRVNAME		"rtc-opal"
22 
23 #include <linux/module.h>
24 #include <linux/err.h>
25 #include <linux/rtc.h>
26 #include <linux/delay.h>
27 #include <linux/bcd.h>
28 #include <linux/platform_device.h>
29 #include <linux/of.h>
30 #include <asm/opal.h>
31 #include <asm/firmware.h>
32 
33 static void opal_to_tm(u32 y_m_d, u64 h_m_s_ms, struct rtc_time *tm)
34 {
35 	tm->tm_year = ((bcd2bin(y_m_d >> 24) * 100) +
36 		       bcd2bin((y_m_d >> 16) & 0xff)) - 1900;
37 	tm->tm_mon  = bcd2bin((y_m_d >> 8) & 0xff) - 1;
38 	tm->tm_mday = bcd2bin(y_m_d & 0xff);
39 	tm->tm_hour = bcd2bin((h_m_s_ms >> 56) & 0xff);
40 	tm->tm_min  = bcd2bin((h_m_s_ms >> 48) & 0xff);
41 	tm->tm_sec  = bcd2bin((h_m_s_ms >> 40) & 0xff);
42 
43 	tm->tm_wday = -1;
44 }
45 
46 static void tm_to_opal(struct rtc_time *tm, u32 *y_m_d, u64 *h_m_s_ms)
47 {
48 	*y_m_d |= ((u32)bin2bcd((tm->tm_year + 1900) / 100)) << 24;
49 	*y_m_d |= ((u32)bin2bcd((tm->tm_year + 1900) % 100)) << 16;
50 	*y_m_d |= ((u32)bin2bcd((tm->tm_mon + 1))) << 8;
51 	*y_m_d |= ((u32)bin2bcd(tm->tm_mday));
52 
53 	*h_m_s_ms |= ((u64)bin2bcd(tm->tm_hour)) << 56;
54 	*h_m_s_ms |= ((u64)bin2bcd(tm->tm_min)) << 48;
55 	*h_m_s_ms |= ((u64)bin2bcd(tm->tm_sec)) << 40;
56 }
57 
58 static int opal_get_rtc_time(struct device *dev, struct rtc_time *tm)
59 {
60 	long rc = OPAL_BUSY;
61 	u32 y_m_d;
62 	u64 h_m_s_ms;
63 	__be32 __y_m_d;
64 	__be64 __h_m_s_ms;
65 
66 	while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
67 		rc = opal_rtc_read(&__y_m_d, &__h_m_s_ms);
68 		if (rc == OPAL_BUSY_EVENT)
69 			opal_poll_events(NULL);
70 		else
71 			msleep(10);
72 	}
73 
74 	if (rc != OPAL_SUCCESS)
75 		return -EIO;
76 
77 	y_m_d = be32_to_cpu(__y_m_d);
78 	h_m_s_ms = be64_to_cpu(__h_m_s_ms);
79 	opal_to_tm(y_m_d, h_m_s_ms, tm);
80 
81 	return 0;
82 }
83 
84 static int opal_set_rtc_time(struct device *dev, struct rtc_time *tm)
85 {
86 	long rc = OPAL_BUSY;
87 	u32 y_m_d = 0;
88 	u64 h_m_s_ms = 0;
89 
90 	tm_to_opal(tm, &y_m_d, &h_m_s_ms);
91 	while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
92 		rc = opal_rtc_write(y_m_d, h_m_s_ms);
93 		if (rc == OPAL_BUSY_EVENT)
94 			opal_poll_events(NULL);
95 		else
96 			msleep(10);
97 	}
98 
99 	return rc == OPAL_SUCCESS ? 0 : -EIO;
100 }
101 
102 /*
103  * TPO	Timed Power-On
104  *
105  * TPO get/set OPAL calls care about the hour and min and to make it consistent
106  * with the rtc utility time conversion functions, we use the 'u64' to store
107  * its value and perform bit shift by 32 before use..
108  */
109 static int opal_get_tpo_time(struct device *dev, struct rtc_wkalrm *alarm)
110 {
111 	__be32 __y_m_d, __h_m;
112 	struct opal_msg msg;
113 	int rc, token;
114 	u64 h_m_s_ms;
115 	u32 y_m_d;
116 
117 	token = opal_async_get_token_interruptible();
118 	if (token < 0) {
119 		if (token != -ERESTARTSYS)
120 			pr_err("Failed to get the async token\n");
121 
122 		return token;
123 	}
124 
125 	rc = opal_tpo_read(token, &__y_m_d, &__h_m);
126 	if (rc != OPAL_ASYNC_COMPLETION) {
127 		rc = -EIO;
128 		goto exit;
129 	}
130 
131 	rc = opal_async_wait_response(token, &msg);
132 	if (rc) {
133 		rc = -EIO;
134 		goto exit;
135 	}
136 
137 	rc = opal_get_async_rc(msg);
138 	if (rc != OPAL_SUCCESS) {
139 		rc = -EIO;
140 		goto exit;
141 	}
142 
143 	y_m_d = be32_to_cpu(__y_m_d);
144 	h_m_s_ms = ((u64)be32_to_cpu(__h_m) << 32);
145 
146 	/* check if no alarm is set */
147 	if (y_m_d == 0 && h_m_s_ms == 0) {
148 		pr_debug("No alarm is set\n");
149 		rc = -ENOENT;
150 		goto exit;
151 	} else {
152 		pr_debug("Alarm set to %x %llx\n", y_m_d, h_m_s_ms);
153 	}
154 
155 	opal_to_tm(y_m_d, h_m_s_ms, &alarm->time);
156 
157 exit:
158 	opal_async_release_token(token);
159 	return rc;
160 }
161 
162 /* Set Timed Power-On */
163 static int opal_set_tpo_time(struct device *dev, struct rtc_wkalrm *alarm)
164 {
165 	u64 h_m_s_ms = 0;
166 	struct opal_msg msg;
167 	u32 y_m_d = 0;
168 	int token, rc;
169 
170 	/* if alarm is enabled */
171 	if (alarm->enabled) {
172 		tm_to_opal(&alarm->time, &y_m_d, &h_m_s_ms);
173 		pr_debug("Alarm set to %x %llx\n", y_m_d, h_m_s_ms);
174 
175 	} else {
176 		pr_debug("Alarm getting disabled\n");
177 	}
178 
179 	token = opal_async_get_token_interruptible();
180 	if (token < 0) {
181 		if (token != -ERESTARTSYS)
182 			pr_err("Failed to get the async token\n");
183 
184 		return token;
185 	}
186 
187 	/* TPO, we care about hour and minute */
188 	rc = opal_tpo_write(token, y_m_d,
189 			    (u32)((h_m_s_ms >> 32) & 0xffff0000));
190 	if (rc != OPAL_ASYNC_COMPLETION) {
191 		rc = -EIO;
192 		goto exit;
193 	}
194 
195 	rc = opal_async_wait_response(token, &msg);
196 	if (rc) {
197 		rc = -EIO;
198 		goto exit;
199 	}
200 
201 	rc = opal_get_async_rc(msg);
202 	if (rc != OPAL_SUCCESS)
203 		rc = -EIO;
204 
205 exit:
206 	opal_async_release_token(token);
207 	return rc;
208 }
209 
210 int opal_tpo_alarm_irq_enable(struct device *dev, unsigned int enabled)
211 {
212 	struct rtc_wkalrm alarm = { .enabled = 0 };
213 
214 	/*
215 	 * TPO is automatically enabled when opal_set_tpo_time() is called with
216 	 * non-zero rtc-time. We only handle disable case which needs to be
217 	 * explicitly told to opal.
218 	 */
219 	return enabled ? 0 : opal_set_tpo_time(dev, &alarm);
220 }
221 
222 static struct rtc_class_ops opal_rtc_ops = {
223 	.read_time	= opal_get_rtc_time,
224 	.set_time	= opal_set_rtc_time,
225 };
226 
227 static int opal_rtc_probe(struct platform_device *pdev)
228 {
229 	struct rtc_device *rtc;
230 
231 	if (pdev->dev.of_node &&
232 	    (of_property_read_bool(pdev->dev.of_node, "wakeup-source") ||
233 	     of_property_read_bool(pdev->dev.of_node, "has-tpo")/* legacy */)) {
234 		device_set_wakeup_capable(&pdev->dev, true);
235 		opal_rtc_ops.read_alarm	= opal_get_tpo_time;
236 		opal_rtc_ops.set_alarm = opal_set_tpo_time;
237 		opal_rtc_ops.alarm_irq_enable = opal_tpo_alarm_irq_enable;
238 	}
239 
240 	rtc = devm_rtc_device_register(&pdev->dev, DRVNAME, &opal_rtc_ops,
241 				       THIS_MODULE);
242 	if (IS_ERR(rtc))
243 		return PTR_ERR(rtc);
244 
245 	rtc->uie_unsupported = 1;
246 
247 	return 0;
248 }
249 
250 static const struct of_device_id opal_rtc_match[] = {
251 	{
252 		.compatible	= "ibm,opal-rtc",
253 	},
254 	{ }
255 };
256 MODULE_DEVICE_TABLE(of, opal_rtc_match);
257 
258 static const struct platform_device_id opal_rtc_driver_ids[] = {
259 	{
260 		.name		= "opal-rtc",
261 	},
262 	{ }
263 };
264 MODULE_DEVICE_TABLE(platform, opal_rtc_driver_ids);
265 
266 static struct platform_driver opal_rtc_driver = {
267 	.probe		= opal_rtc_probe,
268 	.id_table	= opal_rtc_driver_ids,
269 	.driver		= {
270 		.name		= DRVNAME,
271 		.of_match_table	= opal_rtc_match,
272 	},
273 };
274 
275 static int __init opal_rtc_init(void)
276 {
277 	if (!firmware_has_feature(FW_FEATURE_OPAL))
278 		return -ENODEV;
279 
280 	return platform_driver_register(&opal_rtc_driver);
281 }
282 
283 static void __exit opal_rtc_exit(void)
284 {
285 	platform_driver_unregister(&opal_rtc_driver);
286 }
287 
288 MODULE_AUTHOR("Neelesh Gupta <neelegup@linux.vnet.ibm.com>");
289 MODULE_DESCRIPTION("IBM OPAL RTC driver");
290 MODULE_LICENSE("GPL");
291 
292 module_init(opal_rtc_init);
293 module_exit(opal_rtc_exit);
294