rtc-opal.c 6.5 KB

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