rtc-opal.c 5.9 KB

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