blob: 304e891e35fcb060a8ff26f78122fa3c63a1eb87 [file] [log] [blame]
Neelesh Gupta16b1d262014-10-14 14:08:36 +05301/*
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
Joe Perchesa737e832015-04-16 12:46:14 -070019#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
Neelesh Gupta16b1d262014-10-14 14:08:36 +053021#define DRVNAME "rtc-opal"
Neelesh Gupta16b1d262014-10-14 14:08:36 +053022
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
33static 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
Daniel Axtens00b912b2015-12-15 18:09:14 +110043 tm->tm_wday = -1;
Neelesh Gupta16b1d262014-10-14 14:08:36 +053044}
45
46static 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
58static int opal_get_rtc_time(struct device *dev, struct rtc_time *tm)
59{
60 long rc = OPAL_BUSY;
Stewart Smith5b8b5802016-08-02 11:50:16 +100061 int retries = 10;
Neelesh Gupta16b1d262014-10-14 14:08:36 +053062 u32 y_m_d;
63 u64 h_m_s_ms;
64 __be32 __y_m_d;
65 __be64 __h_m_s_ms;
66
67 while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
68 rc = opal_rtc_read(&__y_m_d, &__h_m_s_ms);
69 if (rc == OPAL_BUSY_EVENT)
70 opal_poll_events(NULL);
Stewart Smith5b8b5802016-08-02 11:50:16 +100071 else if (retries-- && (rc == OPAL_HARDWARE
72 || rc == OPAL_INTERNAL_ERROR))
Neelesh Gupta16b1d262014-10-14 14:08:36 +053073 msleep(10);
Stewart Smith5b8b5802016-08-02 11:50:16 +100074 else if (rc != OPAL_BUSY && rc != OPAL_BUSY_EVENT)
75 break;
Neelesh Gupta16b1d262014-10-14 14:08:36 +053076 }
77
78 if (rc != OPAL_SUCCESS)
79 return -EIO;
80
81 y_m_d = be32_to_cpu(__y_m_d);
82 h_m_s_ms = be64_to_cpu(__h_m_s_ms);
83 opal_to_tm(y_m_d, h_m_s_ms, tm);
84
85 return 0;
86}
87
88static int opal_set_rtc_time(struct device *dev, struct rtc_time *tm)
89{
90 long rc = OPAL_BUSY;
Stewart Smith5b8b5802016-08-02 11:50:16 +100091 int retries = 10;
Neelesh Gupta16b1d262014-10-14 14:08:36 +053092 u32 y_m_d = 0;
93 u64 h_m_s_ms = 0;
94
95 tm_to_opal(tm, &y_m_d, &h_m_s_ms);
96 while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
97 rc = opal_rtc_write(y_m_d, h_m_s_ms);
98 if (rc == OPAL_BUSY_EVENT)
99 opal_poll_events(NULL);
Stewart Smith5b8b5802016-08-02 11:50:16 +1000100 else if (retries-- && (rc == OPAL_HARDWARE
101 || rc == OPAL_INTERNAL_ERROR))
Neelesh Gupta16b1d262014-10-14 14:08:36 +0530102 msleep(10);
Stewart Smith5b8b5802016-08-02 11:50:16 +1000103 else if (rc != OPAL_BUSY && rc != OPAL_BUSY_EVENT)
104 break;
Neelesh Gupta16b1d262014-10-14 14:08:36 +0530105 }
106
107 return rc == OPAL_SUCCESS ? 0 : -EIO;
108}
109
110/*
111 * TPO Timed Power-On
112 *
113 * TPO get/set OPAL calls care about the hour and min and to make it consistent
114 * with the rtc utility time conversion functions, we use the 'u64' to store
115 * its value and perform bit shift by 32 before use..
116 */
117static int opal_get_tpo_time(struct device *dev, struct rtc_wkalrm *alarm)
118{
119 __be32 __y_m_d, __h_m;
120 struct opal_msg msg;
121 int rc, token;
122 u64 h_m_s_ms;
123 u32 y_m_d;
124
125 token = opal_async_get_token_interruptible();
126 if (token < 0) {
127 if (token != -ERESTARTSYS)
128 pr_err("Failed to get the async token\n");
129
130 return token;
131 }
132
133 rc = opal_tpo_read(token, &__y_m_d, &__h_m);
134 if (rc != OPAL_ASYNC_COMPLETION) {
135 rc = -EIO;
136 goto exit;
137 }
138
139 rc = opal_async_wait_response(token, &msg);
140 if (rc) {
141 rc = -EIO;
142 goto exit;
143 }
144
Suraj Jitindar Singhd0226d32016-06-29 13:38:38 +1000145 rc = opal_get_async_rc(msg);
Neelesh Gupta16b1d262014-10-14 14:08:36 +0530146 if (rc != OPAL_SUCCESS) {
147 rc = -EIO;
148 goto exit;
149 }
150
151 y_m_d = be32_to_cpu(__y_m_d);
152 h_m_s_ms = ((u64)be32_to_cpu(__h_m) << 32);
Vaibhav Jain6dc1cf62017-05-19 15:35:09 +0530153
154 /* check if no alarm is set */
155 if (y_m_d == 0 && h_m_s_ms == 0) {
156 pr_debug("No alarm is set\n");
157 rc = -ENOENT;
158 goto exit;
159 } else {
160 pr_debug("Alarm set to %x %llx\n", y_m_d, h_m_s_ms);
161 }
162
Neelesh Gupta16b1d262014-10-14 14:08:36 +0530163 opal_to_tm(y_m_d, h_m_s_ms, &alarm->time);
164
165exit:
166 opal_async_release_token(token);
167 return rc;
168}
169
170/* Set Timed Power-On */
171static int opal_set_tpo_time(struct device *dev, struct rtc_wkalrm *alarm)
172{
Andrzej Hajdac3530092015-09-21 15:33:56 +0200173 u64 h_m_s_ms = 0;
Neelesh Gupta16b1d262014-10-14 14:08:36 +0530174 struct opal_msg msg;
175 u32 y_m_d = 0;
Andrzej Hajdac3530092015-09-21 15:33:56 +0200176 int token, rc;
Neelesh Gupta16b1d262014-10-14 14:08:36 +0530177
Vaibhav Jain0ec77692017-05-31 18:39:01 +0530178 /* if alarm is enabled */
179 if (alarm->enabled) {
180 tm_to_opal(&alarm->time, &y_m_d, &h_m_s_ms);
181 pr_debug("Alarm set to %x %llx\n", y_m_d, h_m_s_ms);
182
183 } else {
184 pr_debug("Alarm getting disabled\n");
185 }
Neelesh Gupta16b1d262014-10-14 14:08:36 +0530186
187 token = opal_async_get_token_interruptible();
188 if (token < 0) {
189 if (token != -ERESTARTSYS)
190 pr_err("Failed to get the async token\n");
191
192 return token;
193 }
194
195 /* TPO, we care about hour and minute */
196 rc = opal_tpo_write(token, y_m_d,
197 (u32)((h_m_s_ms >> 32) & 0xffff0000));
198 if (rc != OPAL_ASYNC_COMPLETION) {
199 rc = -EIO;
200 goto exit;
201 }
202
203 rc = opal_async_wait_response(token, &msg);
204 if (rc) {
205 rc = -EIO;
206 goto exit;
207 }
208
Suraj Jitindar Singhd0226d32016-06-29 13:38:38 +1000209 rc = opal_get_async_rc(msg);
Neelesh Gupta16b1d262014-10-14 14:08:36 +0530210 if (rc != OPAL_SUCCESS)
211 rc = -EIO;
212
213exit:
214 opal_async_release_token(token);
215 return rc;
216}
217
Vaibhav Jain0ec77692017-05-31 18:39:01 +0530218int opal_tpo_alarm_irq_enable(struct device *dev, unsigned int enabled)
219{
220 struct rtc_wkalrm alarm = { .enabled = 0 };
221
222 /*
223 * TPO is automatically enabled when opal_set_tpo_time() is called with
224 * non-zero rtc-time. We only handle disable case which needs to be
225 * explicitly told to opal.
226 */
227 return enabled ? 0 : opal_set_tpo_time(dev, &alarm);
228}
229
Vaibhav Jainf4a2eec2015-07-14 13:28:28 +0530230static struct rtc_class_ops opal_rtc_ops = {
Neelesh Gupta16b1d262014-10-14 14:08:36 +0530231 .read_time = opal_get_rtc_time,
232 .set_time = opal_set_rtc_time,
Neelesh Gupta16b1d262014-10-14 14:08:36 +0530233};
234
235static int opal_rtc_probe(struct platform_device *pdev)
236{
237 struct rtc_device *rtc;
238
Sudeep Holla347e40f2015-10-21 11:10:00 +0100239 if (pdev->dev.of_node &&
240 (of_property_read_bool(pdev->dev.of_node, "wakeup-source") ||
241 of_property_read_bool(pdev->dev.of_node, "has-tpo")/* legacy */)) {
Neelesh Gupta16b1d262014-10-14 14:08:36 +0530242 device_set_wakeup_capable(&pdev->dev, true);
Vaibhav Jainf4a2eec2015-07-14 13:28:28 +0530243 opal_rtc_ops.read_alarm = opal_get_tpo_time;
244 opal_rtc_ops.set_alarm = opal_set_tpo_time;
Vaibhav Jain0ec77692017-05-31 18:39:01 +0530245 opal_rtc_ops.alarm_irq_enable = opal_tpo_alarm_irq_enable;
Vaibhav Jainf4a2eec2015-07-14 13:28:28 +0530246 }
Neelesh Gupta16b1d262014-10-14 14:08:36 +0530247
248 rtc = devm_rtc_device_register(&pdev->dev, DRVNAME, &opal_rtc_ops,
249 THIS_MODULE);
250 if (IS_ERR(rtc))
251 return PTR_ERR(rtc);
252
253 rtc->uie_unsupported = 1;
254
255 return 0;
256}
257
258static const struct of_device_id opal_rtc_match[] = {
259 {
260 .compatible = "ibm,opal-rtc",
261 },
262 { }
263};
264MODULE_DEVICE_TABLE(of, opal_rtc_match);
265
266static const struct platform_device_id opal_rtc_driver_ids[] = {
267 {
268 .name = "opal-rtc",
269 },
270 { }
271};
272MODULE_DEVICE_TABLE(platform, opal_rtc_driver_ids);
273
274static struct platform_driver opal_rtc_driver = {
275 .probe = opal_rtc_probe,
276 .id_table = opal_rtc_driver_ids,
277 .driver = {
278 .name = DRVNAME,
Neelesh Gupta16b1d262014-10-14 14:08:36 +0530279 .of_match_table = opal_rtc_match,
280 },
281};
282
283static int __init opal_rtc_init(void)
284{
285 if (!firmware_has_feature(FW_FEATURE_OPAL))
286 return -ENODEV;
287
288 return platform_driver_register(&opal_rtc_driver);
289}
290
291static void __exit opal_rtc_exit(void)
292{
293 platform_driver_unregister(&opal_rtc_driver);
294}
295
296MODULE_AUTHOR("Neelesh Gupta <neelegup@linux.vnet.ibm.com>");
297MODULE_DESCRIPTION("IBM OPAL RTC driver");
298MODULE_LICENSE("GPL");
299
300module_init(opal_rtc_init);
301module_exit(opal_rtc_exit);