0001
0002
0003
0004
0005
0006
0007 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0008
0009 #define DRVNAME "rtc-opal"
0010
0011 #include <linux/module.h>
0012 #include <linux/err.h>
0013 #include <linux/rtc.h>
0014 #include <linux/delay.h>
0015 #include <linux/bcd.h>
0016 #include <linux/platform_device.h>
0017 #include <linux/of.h>
0018 #include <asm/opal.h>
0019 #include <asm/firmware.h>
0020
0021 static void opal_to_tm(u32 y_m_d, u64 h_m_s_ms, struct rtc_time *tm)
0022 {
0023 tm->tm_year = ((bcd2bin(y_m_d >> 24) * 100) +
0024 bcd2bin((y_m_d >> 16) & 0xff)) - 1900;
0025 tm->tm_mon = bcd2bin((y_m_d >> 8) & 0xff) - 1;
0026 tm->tm_mday = bcd2bin(y_m_d & 0xff);
0027 tm->tm_hour = bcd2bin((h_m_s_ms >> 56) & 0xff);
0028 tm->tm_min = bcd2bin((h_m_s_ms >> 48) & 0xff);
0029 tm->tm_sec = bcd2bin((h_m_s_ms >> 40) & 0xff);
0030
0031 tm->tm_wday = -1;
0032 }
0033
0034 static void tm_to_opal(struct rtc_time *tm, u32 *y_m_d, u64 *h_m_s_ms)
0035 {
0036 *y_m_d |= ((u32)bin2bcd((tm->tm_year + 1900) / 100)) << 24;
0037 *y_m_d |= ((u32)bin2bcd((tm->tm_year + 1900) % 100)) << 16;
0038 *y_m_d |= ((u32)bin2bcd((tm->tm_mon + 1))) << 8;
0039 *y_m_d |= ((u32)bin2bcd(tm->tm_mday));
0040
0041 *h_m_s_ms |= ((u64)bin2bcd(tm->tm_hour)) << 56;
0042 *h_m_s_ms |= ((u64)bin2bcd(tm->tm_min)) << 48;
0043 *h_m_s_ms |= ((u64)bin2bcd(tm->tm_sec)) << 40;
0044 }
0045
0046 static int opal_get_rtc_time(struct device *dev, struct rtc_time *tm)
0047 {
0048 s64 rc = OPAL_BUSY;
0049 int retries = 10;
0050 u32 y_m_d;
0051 u64 h_m_s_ms;
0052 __be32 __y_m_d;
0053 __be64 __h_m_s_ms;
0054
0055 while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
0056 rc = opal_rtc_read(&__y_m_d, &__h_m_s_ms);
0057 if (rc == OPAL_BUSY_EVENT) {
0058 msleep(OPAL_BUSY_DELAY_MS);
0059 opal_poll_events(NULL);
0060 } else if (rc == OPAL_BUSY) {
0061 msleep(OPAL_BUSY_DELAY_MS);
0062 } else if (rc == OPAL_HARDWARE || rc == OPAL_INTERNAL_ERROR) {
0063 if (retries--) {
0064 msleep(10);
0065 rc = OPAL_BUSY;
0066 }
0067 }
0068 }
0069
0070 if (rc != OPAL_SUCCESS)
0071 return -EIO;
0072
0073 y_m_d = be32_to_cpu(__y_m_d);
0074 h_m_s_ms = be64_to_cpu(__h_m_s_ms);
0075 opal_to_tm(y_m_d, h_m_s_ms, tm);
0076
0077 return 0;
0078 }
0079
0080 static int opal_set_rtc_time(struct device *dev, struct rtc_time *tm)
0081 {
0082 s64 rc = OPAL_BUSY;
0083 int retries = 10;
0084 u32 y_m_d = 0;
0085 u64 h_m_s_ms = 0;
0086
0087 tm_to_opal(tm, &y_m_d, &h_m_s_ms);
0088
0089 while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
0090 rc = opal_rtc_write(y_m_d, h_m_s_ms);
0091 if (rc == OPAL_BUSY_EVENT) {
0092 msleep(OPAL_BUSY_DELAY_MS);
0093 opal_poll_events(NULL);
0094 } else if (rc == OPAL_BUSY) {
0095 msleep(OPAL_BUSY_DELAY_MS);
0096 } else if (rc == OPAL_HARDWARE || rc == OPAL_INTERNAL_ERROR) {
0097 if (retries--) {
0098 msleep(10);
0099 rc = OPAL_BUSY;
0100 }
0101 }
0102 }
0103
0104 return rc == OPAL_SUCCESS ? 0 : -EIO;
0105 }
0106
0107
0108
0109
0110
0111
0112
0113
0114 static int opal_get_tpo_time(struct device *dev, struct rtc_wkalrm *alarm)
0115 {
0116 __be32 __y_m_d, __h_m;
0117 struct opal_msg msg;
0118 int rc, token;
0119 u64 h_m_s_ms;
0120 u32 y_m_d;
0121
0122 token = opal_async_get_token_interruptible();
0123 if (token < 0) {
0124 if (token != -ERESTARTSYS)
0125 pr_err("Failed to get the async token\n");
0126
0127 return token;
0128 }
0129
0130 rc = opal_tpo_read(token, &__y_m_d, &__h_m);
0131 if (rc != OPAL_ASYNC_COMPLETION) {
0132 rc = -EIO;
0133 goto exit;
0134 }
0135
0136 rc = opal_async_wait_response(token, &msg);
0137 if (rc) {
0138 rc = -EIO;
0139 goto exit;
0140 }
0141
0142 rc = opal_get_async_rc(msg);
0143 if (rc != OPAL_SUCCESS) {
0144 rc = -EIO;
0145 goto exit;
0146 }
0147
0148 y_m_d = be32_to_cpu(__y_m_d);
0149 h_m_s_ms = ((u64)be32_to_cpu(__h_m) << 32);
0150
0151
0152 if (y_m_d == 0 && h_m_s_ms == 0) {
0153 pr_debug("No alarm is set\n");
0154 rc = -ENOENT;
0155 goto exit;
0156 } else {
0157 pr_debug("Alarm set to %x %llx\n", y_m_d, h_m_s_ms);
0158 }
0159
0160 opal_to_tm(y_m_d, h_m_s_ms, &alarm->time);
0161
0162 exit:
0163 opal_async_release_token(token);
0164 return rc;
0165 }
0166
0167
0168 static int opal_set_tpo_time(struct device *dev, struct rtc_wkalrm *alarm)
0169 {
0170 u64 h_m_s_ms = 0;
0171 struct opal_msg msg;
0172 u32 y_m_d = 0;
0173 int token, rc;
0174
0175
0176 if (alarm->enabled) {
0177 tm_to_opal(&alarm->time, &y_m_d, &h_m_s_ms);
0178 pr_debug("Alarm set to %x %llx\n", y_m_d, h_m_s_ms);
0179
0180 } else {
0181 pr_debug("Alarm getting disabled\n");
0182 }
0183
0184 token = opal_async_get_token_interruptible();
0185 if (token < 0) {
0186 if (token != -ERESTARTSYS)
0187 pr_err("Failed to get the async token\n");
0188
0189 return token;
0190 }
0191
0192
0193 rc = opal_tpo_write(token, y_m_d,
0194 (u32)((h_m_s_ms >> 32) & 0xffff0000));
0195 if (rc != OPAL_ASYNC_COMPLETION) {
0196 rc = -EIO;
0197 goto exit;
0198 }
0199
0200 rc = opal_async_wait_response(token, &msg);
0201 if (rc) {
0202 rc = -EIO;
0203 goto exit;
0204 }
0205
0206 rc = opal_get_async_rc(msg);
0207 if (rc != OPAL_SUCCESS)
0208 rc = -EIO;
0209
0210 exit:
0211 opal_async_release_token(token);
0212 return rc;
0213 }
0214
0215 static int opal_tpo_alarm_irq_enable(struct device *dev, unsigned int enabled)
0216 {
0217 struct rtc_wkalrm alarm = { .enabled = 0 };
0218
0219
0220
0221
0222
0223
0224 return enabled ? 0 : opal_set_tpo_time(dev, &alarm);
0225 }
0226
0227 static const struct rtc_class_ops opal_rtc_ops = {
0228 .read_time = opal_get_rtc_time,
0229 .set_time = opal_set_rtc_time,
0230 .read_alarm = opal_get_tpo_time,
0231 .set_alarm = opal_set_tpo_time,
0232 .alarm_irq_enable = opal_tpo_alarm_irq_enable,
0233 };
0234
0235 static int opal_rtc_probe(struct platform_device *pdev)
0236 {
0237 struct rtc_device *rtc;
0238
0239 rtc = devm_rtc_allocate_device(&pdev->dev);
0240 if (IS_ERR(rtc))
0241 return PTR_ERR(rtc);
0242
0243 if (pdev->dev.of_node &&
0244 (of_property_read_bool(pdev->dev.of_node, "wakeup-source") ||
0245 of_property_read_bool(pdev->dev.of_node, "has-tpo")))
0246 device_set_wakeup_capable(&pdev->dev, true);
0247 else
0248 clear_bit(RTC_FEATURE_ALARM, rtc->features);
0249
0250 rtc->ops = &opal_rtc_ops;
0251 rtc->range_min = RTC_TIMESTAMP_BEGIN_0000;
0252 rtc->range_max = RTC_TIMESTAMP_END_9999;
0253 clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, rtc->features);
0254
0255 return devm_rtc_register_device(rtc);
0256 }
0257
0258 static const struct of_device_id opal_rtc_match[] = {
0259 {
0260 .compatible = "ibm,opal-rtc",
0261 },
0262 { }
0263 };
0264 MODULE_DEVICE_TABLE(of, opal_rtc_match);
0265
0266 static const struct platform_device_id opal_rtc_driver_ids[] = {
0267 {
0268 .name = "opal-rtc",
0269 },
0270 { }
0271 };
0272 MODULE_DEVICE_TABLE(platform, opal_rtc_driver_ids);
0273
0274 static struct platform_driver opal_rtc_driver = {
0275 .probe = opal_rtc_probe,
0276 .id_table = opal_rtc_driver_ids,
0277 .driver = {
0278 .name = DRVNAME,
0279 .of_match_table = opal_rtc_match,
0280 },
0281 };
0282
0283 static int __init opal_rtc_init(void)
0284 {
0285 if (!firmware_has_feature(FW_FEATURE_OPAL))
0286 return -ENODEV;
0287
0288 return platform_driver_register(&opal_rtc_driver);
0289 }
0290
0291 static void __exit opal_rtc_exit(void)
0292 {
0293 platform_driver_unregister(&opal_rtc_driver);
0294 }
0295
0296 MODULE_AUTHOR("Neelesh Gupta <neelegup@linux.vnet.ibm.com>");
0297 MODULE_DESCRIPTION("IBM OPAL RTC driver");
0298 MODULE_LICENSE("GPL");
0299
0300 module_init(opal_rtc_init);
0301 module_exit(opal_rtc_exit);