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