Blame view

drivers/rtc/rtc-opal.c 6.73 KB
d5bb994bc   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
16b1d26e7   Neelesh Gupta   rtc/tpo: Driver t...
2
3
4
  /*
   * IBM OPAL RTC driver
   * Copyright (C) 2014 IBM
16b1d26e7   Neelesh Gupta   rtc/tpo: Driver t...
5
   */
a737e835e   Joe Perches   rtc: use more sta...
6
  #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16b1d26e7   Neelesh Gupta   rtc/tpo: Driver t...
7
  #define DRVNAME		"rtc-opal"
16b1d26e7   Neelesh Gupta   rtc/tpo: Driver t...
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  
  #include <linux/module.h>
  #include <linux/err.h>
  #include <linux/rtc.h>
  #include <linux/delay.h>
  #include <linux/bcd.h>
  #include <linux/platform_device.h>
  #include <linux/of.h>
  #include <asm/opal.h>
  #include <asm/firmware.h>
  
  static void opal_to_tm(u32 y_m_d, u64 h_m_s_ms, struct rtc_time *tm)
  {
  	tm->tm_year = ((bcd2bin(y_m_d >> 24) * 100) +
  		       bcd2bin((y_m_d >> 16) & 0xff)) - 1900;
  	tm->tm_mon  = bcd2bin((y_m_d >> 8) & 0xff) - 1;
  	tm->tm_mday = bcd2bin(y_m_d & 0xff);
  	tm->tm_hour = bcd2bin((h_m_s_ms >> 56) & 0xff);
  	tm->tm_min  = bcd2bin((h_m_s_ms >> 48) & 0xff);
  	tm->tm_sec  = bcd2bin((h_m_s_ms >> 40) & 0xff);
00b912b0c   Daniel Axtens   powerpc: Remove b...
28
  	tm->tm_wday = -1;
16b1d26e7   Neelesh Gupta   rtc/tpo: Driver t...
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  }
  
  static void tm_to_opal(struct rtc_time *tm, u32 *y_m_d, u64 *h_m_s_ms)
  {
  	*y_m_d |= ((u32)bin2bcd((tm->tm_year + 1900) / 100)) << 24;
  	*y_m_d |= ((u32)bin2bcd((tm->tm_year + 1900) % 100)) << 16;
  	*y_m_d |= ((u32)bin2bcd((tm->tm_mon + 1))) << 8;
  	*y_m_d |= ((u32)bin2bcd(tm->tm_mday));
  
  	*h_m_s_ms |= ((u64)bin2bcd(tm->tm_hour)) << 56;
  	*h_m_s_ms |= ((u64)bin2bcd(tm->tm_min)) << 48;
  	*h_m_s_ms |= ((u64)bin2bcd(tm->tm_sec)) << 40;
  }
  
  static int opal_get_rtc_time(struct device *dev, struct rtc_time *tm)
  {
682e6b4da   Nicholas Piggin   rtc: opal: Fix OP...
45
  	s64 rc = OPAL_BUSY;
5b8b58063   Stewart Smith   rtc-opal: Fix han...
46
  	int retries = 10;
16b1d26e7   Neelesh Gupta   rtc/tpo: Driver t...
47
48
49
50
51
52
53
  	u32 y_m_d;
  	u64 h_m_s_ms;
  	__be32 __y_m_d;
  	__be64 __h_m_s_ms;
  
  	while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
  		rc = opal_rtc_read(&__y_m_d, &__h_m_s_ms);
682e6b4da   Nicholas Piggin   rtc: opal: Fix OP...
54
55
  		if (rc == OPAL_BUSY_EVENT) {
  			msleep(OPAL_BUSY_DELAY_MS);
16b1d26e7   Neelesh Gupta   rtc/tpo: Driver t...
56
  			opal_poll_events(NULL);
682e6b4da   Nicholas Piggin   rtc: opal: Fix OP...
57
58
59
60
61
62
63
64
  		} else if (rc == OPAL_BUSY) {
  			msleep(OPAL_BUSY_DELAY_MS);
  		} else if (rc == OPAL_HARDWARE || rc == OPAL_INTERNAL_ERROR) {
  			if (retries--) {
  				msleep(10); /* Wait 10ms before retry */
  				rc = OPAL_BUSY; /* go around again */
  			}
  		}
16b1d26e7   Neelesh Gupta   rtc/tpo: Driver t...
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  	}
  
  	if (rc != OPAL_SUCCESS)
  		return -EIO;
  
  	y_m_d = be32_to_cpu(__y_m_d);
  	h_m_s_ms = be64_to_cpu(__h_m_s_ms);
  	opal_to_tm(y_m_d, h_m_s_ms, tm);
  
  	return 0;
  }
  
  static int opal_set_rtc_time(struct device *dev, struct rtc_time *tm)
  {
682e6b4da   Nicholas Piggin   rtc: opal: Fix OP...
79
  	s64 rc = OPAL_BUSY;
5b8b58063   Stewart Smith   rtc-opal: Fix han...
80
  	int retries = 10;
16b1d26e7   Neelesh Gupta   rtc/tpo: Driver t...
81
82
83
84
  	u32 y_m_d = 0;
  	u64 h_m_s_ms = 0;
  
  	tm_to_opal(tm, &y_m_d, &h_m_s_ms);
682e6b4da   Nicholas Piggin   rtc: opal: Fix OP...
85

16b1d26e7   Neelesh Gupta   rtc/tpo: Driver t...
86
87
  	while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
  		rc = opal_rtc_write(y_m_d, h_m_s_ms);
682e6b4da   Nicholas Piggin   rtc: opal: Fix OP...
88
89
  		if (rc == OPAL_BUSY_EVENT) {
  			msleep(OPAL_BUSY_DELAY_MS);
16b1d26e7   Neelesh Gupta   rtc/tpo: Driver t...
90
  			opal_poll_events(NULL);
682e6b4da   Nicholas Piggin   rtc: opal: Fix OP...
91
92
93
94
95
96
97
98
  		} else if (rc == OPAL_BUSY) {
  			msleep(OPAL_BUSY_DELAY_MS);
  		} else if (rc == OPAL_HARDWARE || rc == OPAL_INTERNAL_ERROR) {
  			if (retries--) {
  				msleep(10); /* Wait 10ms before retry */
  				rc = OPAL_BUSY; /* go around again */
  			}
  		}
16b1d26e7   Neelesh Gupta   rtc/tpo: Driver t...
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
  	}
  
  	return rc == OPAL_SUCCESS ? 0 : -EIO;
  }
  
  /*
   * TPO	Timed Power-On
   *
   * TPO get/set OPAL calls care about the hour and min and to make it consistent
   * with the rtc utility time conversion functions, we use the 'u64' to store
   * its value and perform bit shift by 32 before use..
   */
  static int opal_get_tpo_time(struct device *dev, struct rtc_wkalrm *alarm)
  {
  	__be32 __y_m_d, __h_m;
  	struct opal_msg msg;
  	int rc, token;
  	u64 h_m_s_ms;
  	u32 y_m_d;
  
  	token = opal_async_get_token_interruptible();
  	if (token < 0) {
  		if (token != -ERESTARTSYS)
  			pr_err("Failed to get the async token
  ");
  
  		return token;
  	}
  
  	rc = opal_tpo_read(token, &__y_m_d, &__h_m);
  	if (rc != OPAL_ASYNC_COMPLETION) {
  		rc = -EIO;
  		goto exit;
  	}
  
  	rc = opal_async_wait_response(token, &msg);
  	if (rc) {
  		rc = -EIO;
  		goto exit;
  	}
d0226d315   Suraj Jitindar Singh   powerpc/opal: Add...
139
  	rc = opal_get_async_rc(msg);
16b1d26e7   Neelesh Gupta   rtc/tpo: Driver t...
140
141
142
143
144
145
146
  	if (rc != OPAL_SUCCESS) {
  		rc = -EIO;
  		goto exit;
  	}
  
  	y_m_d = be32_to_cpu(__y_m_d);
  	h_m_s_ms = ((u64)be32_to_cpu(__h_m) << 32);
6dc1cf6f9   Vaibhav Jain   rtc: opal: Handle...
147
148
149
150
151
152
153
154
155
156
157
  
  	/* check if no alarm is set */
  	if (y_m_d == 0 && h_m_s_ms == 0) {
  		pr_debug("No alarm is set
  ");
  		rc = -ENOENT;
  		goto exit;
  	} else {
  		pr_debug("Alarm set to %x %llx
  ", y_m_d, h_m_s_ms);
  	}
16b1d26e7   Neelesh Gupta   rtc/tpo: Driver t...
158
159
160
161
162
163
164
165
166
167
  	opal_to_tm(y_m_d, h_m_s_ms, &alarm->time);
  
  exit:
  	opal_async_release_token(token);
  	return rc;
  }
  
  /* Set Timed Power-On */
  static int opal_set_tpo_time(struct device *dev, struct rtc_wkalrm *alarm)
  {
c35300941   Andrzej Hajda   rtc: opal: fix ty...
168
  	u64 h_m_s_ms = 0;
16b1d26e7   Neelesh Gupta   rtc/tpo: Driver t...
169
170
  	struct opal_msg msg;
  	u32 y_m_d = 0;
c35300941   Andrzej Hajda   rtc: opal: fix ty...
171
  	int token, rc;
16b1d26e7   Neelesh Gupta   rtc/tpo: Driver t...
172

0ec7769a9   Vaibhav Jain   rtc: opal: Implem...
173
174
175
176
177
178
179
180
181
182
  	/* if alarm is enabled */
  	if (alarm->enabled) {
  		tm_to_opal(&alarm->time, &y_m_d, &h_m_s_ms);
  		pr_debug("Alarm set to %x %llx
  ", y_m_d, h_m_s_ms);
  
  	} else {
  		pr_debug("Alarm getting disabled
  ");
  	}
16b1d26e7   Neelesh Gupta   rtc/tpo: Driver t...
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
  
  	token = opal_async_get_token_interruptible();
  	if (token < 0) {
  		if (token != -ERESTARTSYS)
  			pr_err("Failed to get the async token
  ");
  
  		return token;
  	}
  
  	/* TPO, we care about hour and minute */
  	rc = opal_tpo_write(token, y_m_d,
  			    (u32)((h_m_s_ms >> 32) & 0xffff0000));
  	if (rc != OPAL_ASYNC_COMPLETION) {
  		rc = -EIO;
  		goto exit;
  	}
  
  	rc = opal_async_wait_response(token, &msg);
  	if (rc) {
  		rc = -EIO;
  		goto exit;
  	}
d0226d315   Suraj Jitindar Singh   powerpc/opal: Add...
206
  	rc = opal_get_async_rc(msg);
16b1d26e7   Neelesh Gupta   rtc/tpo: Driver t...
207
208
209
210
211
212
213
  	if (rc != OPAL_SUCCESS)
  		rc = -EIO;
  
  exit:
  	opal_async_release_token(token);
  	return rc;
  }
fd86b2d4b   YueHaibing   rtc: opal: Make o...
214
  static int opal_tpo_alarm_irq_enable(struct device *dev, unsigned int enabled)
0ec7769a9   Vaibhav Jain   rtc: opal: Implem...
215
216
217
218
219
220
221
222
223
224
  {
  	struct rtc_wkalrm alarm = { .enabled = 0 };
  
  	/*
  	 * TPO is automatically enabled when opal_set_tpo_time() is called with
  	 * non-zero rtc-time. We only handle disable case which needs to be
  	 * explicitly told to opal.
  	 */
  	return enabled ? 0 : opal_set_tpo_time(dev, &alarm);
  }
f4a2eecb3   Vaibhav Jain   rtc: opal: Enable...
225
  static struct rtc_class_ops opal_rtc_ops = {
16b1d26e7   Neelesh Gupta   rtc/tpo: Driver t...
226
227
  	.read_time	= opal_get_rtc_time,
  	.set_time	= opal_set_rtc_time,
16b1d26e7   Neelesh Gupta   rtc/tpo: Driver t...
228
229
230
231
232
  };
  
  static int opal_rtc_probe(struct platform_device *pdev)
  {
  	struct rtc_device *rtc;
347e40f03   Sudeep Holla   rtc: opal: enable...
233
234
235
  	if (pdev->dev.of_node &&
  	    (of_property_read_bool(pdev->dev.of_node, "wakeup-source") ||
  	     of_property_read_bool(pdev->dev.of_node, "has-tpo")/* legacy */)) {
16b1d26e7   Neelesh Gupta   rtc/tpo: Driver t...
236
  		device_set_wakeup_capable(&pdev->dev, true);
f4a2eecb3   Vaibhav Jain   rtc: opal: Enable...
237
238
  		opal_rtc_ops.read_alarm	= opal_get_tpo_time;
  		opal_rtc_ops.set_alarm = opal_set_tpo_time;
0ec7769a9   Vaibhav Jain   rtc: opal: Implem...
239
  		opal_rtc_ops.alarm_irq_enable = opal_tpo_alarm_irq_enable;
f4a2eecb3   Vaibhav Jain   rtc: opal: Enable...
240
  	}
16b1d26e7   Neelesh Gupta   rtc/tpo: Driver t...
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
  
  	rtc = devm_rtc_device_register(&pdev->dev, DRVNAME, &opal_rtc_ops,
  				       THIS_MODULE);
  	if (IS_ERR(rtc))
  		return PTR_ERR(rtc);
  
  	rtc->uie_unsupported = 1;
  
  	return 0;
  }
  
  static const struct of_device_id opal_rtc_match[] = {
  	{
  		.compatible	= "ibm,opal-rtc",
  	},
  	{ }
  };
  MODULE_DEVICE_TABLE(of, opal_rtc_match);
  
  static const struct platform_device_id opal_rtc_driver_ids[] = {
  	{
  		.name		= "opal-rtc",
  	},
  	{ }
  };
  MODULE_DEVICE_TABLE(platform, opal_rtc_driver_ids);
  
  static struct platform_driver opal_rtc_driver = {
  	.probe		= opal_rtc_probe,
  	.id_table	= opal_rtc_driver_ids,
  	.driver		= {
  		.name		= DRVNAME,
16b1d26e7   Neelesh Gupta   rtc/tpo: Driver t...
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
  		.of_match_table	= opal_rtc_match,
  	},
  };
  
  static int __init opal_rtc_init(void)
  {
  	if (!firmware_has_feature(FW_FEATURE_OPAL))
  		return -ENODEV;
  
  	return platform_driver_register(&opal_rtc_driver);
  }
  
  static void __exit opal_rtc_exit(void)
  {
  	platform_driver_unregister(&opal_rtc_driver);
  }
  
  MODULE_AUTHOR("Neelesh Gupta <neelegup@linux.vnet.ibm.com>");
  MODULE_DESCRIPTION("IBM OPAL RTC driver");
  MODULE_LICENSE("GPL");
  
  module_init(opal_rtc_init);
  module_exit(opal_rtc_exit);