rtc-opal.c 6.9 KB

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