Neelesh Gupta | 16b1d26 | 2014-10-14 14:08:36 +0530 | [diff] [blame] | 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 | |
Joe Perches | a737e83 | 2015-04-16 12:46:14 -0700 | [diff] [blame] | 19 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 20 | |
Neelesh Gupta | 16b1d26 | 2014-10-14 14:08:36 +0530 | [diff] [blame] | 21 | #define DRVNAME "rtc-opal" |
Neelesh Gupta | 16b1d26 | 2014-10-14 14:08:36 +0530 | [diff] [blame] | 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 | |
Daniel Axtens | 00b912b | 2015-12-15 18:09:14 +1100 | [diff] [blame] | 43 | tm->tm_wday = -1; |
Neelesh Gupta | 16b1d26 | 2014-10-14 14:08:36 +0530 | [diff] [blame] | 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 | { |
Nicholas Piggin | a2a8b26 | 2018-04-10 21:49:32 +1000 | [diff] [blame] | 60 | s64 rc = OPAL_BUSY; |
Stewart Smith | b7dc0f5 | 2016-08-02 11:50:16 +1000 | [diff] [blame] | 61 | int retries = 10; |
Neelesh Gupta | 16b1d26 | 2014-10-14 14:08:36 +0530 | [diff] [blame] | 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); |
Nicholas Piggin | a2a8b26 | 2018-04-10 21:49:32 +1000 | [diff] [blame] | 69 | if (rc == OPAL_BUSY_EVENT) { |
| 70 | msleep(OPAL_BUSY_DELAY_MS); |
Neelesh Gupta | 16b1d26 | 2014-10-14 14:08:36 +0530 | [diff] [blame] | 71 | opal_poll_events(NULL); |
Nicholas Piggin | a2a8b26 | 2018-04-10 21:49:32 +1000 | [diff] [blame] | 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 | } |
Neelesh Gupta | 16b1d26 | 2014-10-14 14:08:36 +0530 | [diff] [blame] | 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 | |
| 92 | static int opal_set_rtc_time(struct device *dev, struct rtc_time *tm) |
| 93 | { |
Nicholas Piggin | a2a8b26 | 2018-04-10 21:49:32 +1000 | [diff] [blame] | 94 | s64 rc = OPAL_BUSY; |
Stewart Smith | b7dc0f5 | 2016-08-02 11:50:16 +1000 | [diff] [blame] | 95 | int retries = 10; |
Neelesh Gupta | 16b1d26 | 2014-10-14 14:08:36 +0530 | [diff] [blame] | 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); |
Nicholas Piggin | a2a8b26 | 2018-04-10 21:49:32 +1000 | [diff] [blame] | 100 | |
Neelesh Gupta | 16b1d26 | 2014-10-14 14:08:36 +0530 | [diff] [blame] | 101 | while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) { |
| 102 | rc = opal_rtc_write(y_m_d, h_m_s_ms); |
Nicholas Piggin | a2a8b26 | 2018-04-10 21:49:32 +1000 | [diff] [blame] | 103 | if (rc == OPAL_BUSY_EVENT) { |
| 104 | msleep(OPAL_BUSY_DELAY_MS); |
Neelesh Gupta | 16b1d26 | 2014-10-14 14:08:36 +0530 | [diff] [blame] | 105 | opal_poll_events(NULL); |
Nicholas Piggin | a2a8b26 | 2018-04-10 21:49:32 +1000 | [diff] [blame] | 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 | } |
Neelesh Gupta | 16b1d26 | 2014-10-14 14:08:36 +0530 | [diff] [blame] | 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 | */ |
| 126 | static 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 | |
Suraj Jitindar Singh | d0226d3 | 2016-06-29 13:38:38 +1000 | [diff] [blame] | 154 | rc = opal_get_async_rc(msg); |
Neelesh Gupta | 16b1d26 | 2014-10-14 14:08:36 +0530 | [diff] [blame] | 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); |
Vaibhav Jain | e1dcd00 | 2017-05-19 15:35:09 +0530 | [diff] [blame] | 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 | |
Neelesh Gupta | 16b1d26 | 2014-10-14 14:08:36 +0530 | [diff] [blame] | 172 | opal_to_tm(y_m_d, h_m_s_ms, &alarm->time); |
| 173 | |
| 174 | exit: |
| 175 | opal_async_release_token(token); |
| 176 | return rc; |
| 177 | } |
| 178 | |
| 179 | /* Set Timed Power-On */ |
| 180 | static int opal_set_tpo_time(struct device *dev, struct rtc_wkalrm *alarm) |
| 181 | { |
Andrzej Hajda | c353009 | 2015-09-21 15:33:56 +0200 | [diff] [blame] | 182 | u64 h_m_s_ms = 0; |
Neelesh Gupta | 16b1d26 | 2014-10-14 14:08:36 +0530 | [diff] [blame] | 183 | struct opal_msg msg; |
| 184 | u32 y_m_d = 0; |
Andrzej Hajda | c353009 | 2015-09-21 15:33:56 +0200 | [diff] [blame] | 185 | int token, rc; |
Neelesh Gupta | 16b1d26 | 2014-10-14 14:08:36 +0530 | [diff] [blame] | 186 | |
| 187 | tm_to_opal(&alarm->time, &y_m_d, &h_m_s_ms); |
| 188 | |
| 189 | token = opal_async_get_token_interruptible(); |
| 190 | if (token < 0) { |
| 191 | if (token != -ERESTARTSYS) |
| 192 | pr_err("Failed to get the async token\n"); |
| 193 | |
| 194 | return token; |
| 195 | } |
| 196 | |
| 197 | /* TPO, we care about hour and minute */ |
| 198 | rc = opal_tpo_write(token, y_m_d, |
| 199 | (u32)((h_m_s_ms >> 32) & 0xffff0000)); |
| 200 | if (rc != OPAL_ASYNC_COMPLETION) { |
| 201 | rc = -EIO; |
| 202 | goto exit; |
| 203 | } |
| 204 | |
| 205 | rc = opal_async_wait_response(token, &msg); |
| 206 | if (rc) { |
| 207 | rc = -EIO; |
| 208 | goto exit; |
| 209 | } |
| 210 | |
Suraj Jitindar Singh | d0226d3 | 2016-06-29 13:38:38 +1000 | [diff] [blame] | 211 | rc = opal_get_async_rc(msg); |
Neelesh Gupta | 16b1d26 | 2014-10-14 14:08:36 +0530 | [diff] [blame] | 212 | if (rc != OPAL_SUCCESS) |
| 213 | rc = -EIO; |
| 214 | |
| 215 | exit: |
| 216 | opal_async_release_token(token); |
| 217 | return rc; |
| 218 | } |
| 219 | |
Vaibhav Jain | f4a2eec | 2015-07-14 13:28:28 +0530 | [diff] [blame] | 220 | static struct rtc_class_ops opal_rtc_ops = { |
Neelesh Gupta | 16b1d26 | 2014-10-14 14:08:36 +0530 | [diff] [blame] | 221 | .read_time = opal_get_rtc_time, |
| 222 | .set_time = opal_set_rtc_time, |
Neelesh Gupta | 16b1d26 | 2014-10-14 14:08:36 +0530 | [diff] [blame] | 223 | }; |
| 224 | |
| 225 | static int opal_rtc_probe(struct platform_device *pdev) |
| 226 | { |
| 227 | struct rtc_device *rtc; |
| 228 | |
Sudeep Holla | 347e40f | 2015-10-21 11:10:00 +0100 | [diff] [blame] | 229 | if (pdev->dev.of_node && |
| 230 | (of_property_read_bool(pdev->dev.of_node, "wakeup-source") || |
| 231 | of_property_read_bool(pdev->dev.of_node, "has-tpo")/* legacy */)) { |
Neelesh Gupta | 16b1d26 | 2014-10-14 14:08:36 +0530 | [diff] [blame] | 232 | device_set_wakeup_capable(&pdev->dev, true); |
Vaibhav Jain | f4a2eec | 2015-07-14 13:28:28 +0530 | [diff] [blame] | 233 | opal_rtc_ops.read_alarm = opal_get_tpo_time; |
| 234 | opal_rtc_ops.set_alarm = opal_set_tpo_time; |
| 235 | } |
Neelesh Gupta | 16b1d26 | 2014-10-14 14:08:36 +0530 | [diff] [blame] | 236 | |
| 237 | rtc = devm_rtc_device_register(&pdev->dev, DRVNAME, &opal_rtc_ops, |
| 238 | THIS_MODULE); |
| 239 | if (IS_ERR(rtc)) |
| 240 | return PTR_ERR(rtc); |
| 241 | |
| 242 | rtc->uie_unsupported = 1; |
| 243 | |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | static const struct of_device_id opal_rtc_match[] = { |
| 248 | { |
| 249 | .compatible = "ibm,opal-rtc", |
| 250 | }, |
| 251 | { } |
| 252 | }; |
| 253 | MODULE_DEVICE_TABLE(of, opal_rtc_match); |
| 254 | |
| 255 | static const struct platform_device_id opal_rtc_driver_ids[] = { |
| 256 | { |
| 257 | .name = "opal-rtc", |
| 258 | }, |
| 259 | { } |
| 260 | }; |
| 261 | MODULE_DEVICE_TABLE(platform, opal_rtc_driver_ids); |
| 262 | |
| 263 | static struct platform_driver opal_rtc_driver = { |
| 264 | .probe = opal_rtc_probe, |
| 265 | .id_table = opal_rtc_driver_ids, |
| 266 | .driver = { |
| 267 | .name = DRVNAME, |
Neelesh Gupta | 16b1d26 | 2014-10-14 14:08:36 +0530 | [diff] [blame] | 268 | .of_match_table = opal_rtc_match, |
| 269 | }, |
| 270 | }; |
| 271 | |
| 272 | static int __init opal_rtc_init(void) |
| 273 | { |
| 274 | if (!firmware_has_feature(FW_FEATURE_OPAL)) |
| 275 | return -ENODEV; |
| 276 | |
| 277 | return platform_driver_register(&opal_rtc_driver); |
| 278 | } |
| 279 | |
| 280 | static void __exit opal_rtc_exit(void) |
| 281 | { |
| 282 | platform_driver_unregister(&opal_rtc_driver); |
| 283 | } |
| 284 | |
| 285 | MODULE_AUTHOR("Neelesh Gupta <neelegup@linux.vnet.ibm.com>"); |
| 286 | MODULE_DESCRIPTION("IBM OPAL RTC driver"); |
| 287 | MODULE_LICENSE("GPL"); |
| 288 | |
| 289 | module_init(opal_rtc_init); |
| 290 | module_exit(opal_rtc_exit); |