rtc-opal.c 6.7 KB

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