rtc-opal.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. /* if alarm is enabled */
  160. if (alarm->enabled) {
  161. tm_to_opal(&alarm->time, &y_m_d, &h_m_s_ms);
  162. pr_debug("Alarm set to %x %llx\n", y_m_d, h_m_s_ms);
  163. } else {
  164. pr_debug("Alarm getting disabled\n");
  165. }
  166. token = opal_async_get_token_interruptible();
  167. if (token < 0) {
  168. if (token != -ERESTARTSYS)
  169. pr_err("Failed to get the async token\n");
  170. return token;
  171. }
  172. /* TPO, we care about hour and minute */
  173. rc = opal_tpo_write(token, y_m_d,
  174. (u32)((h_m_s_ms >> 32) & 0xffff0000));
  175. if (rc != OPAL_ASYNC_COMPLETION) {
  176. rc = -EIO;
  177. goto exit;
  178. }
  179. rc = opal_async_wait_response(token, &msg);
  180. if (rc) {
  181. rc = -EIO;
  182. goto exit;
  183. }
  184. rc = opal_get_async_rc(msg);
  185. if (rc != OPAL_SUCCESS)
  186. rc = -EIO;
  187. exit:
  188. opal_async_release_token(token);
  189. return rc;
  190. }
  191. int opal_tpo_alarm_irq_enable(struct device *dev, unsigned int enabled)
  192. {
  193. struct rtc_wkalrm alarm = { .enabled = 0 };
  194. /*
  195. * TPO is automatically enabled when opal_set_tpo_time() is called with
  196. * non-zero rtc-time. We only handle disable case which needs to be
  197. * explicitly told to opal.
  198. */
  199. return enabled ? 0 : opal_set_tpo_time(dev, &alarm);
  200. }
  201. static struct rtc_class_ops opal_rtc_ops = {
  202. .read_time = opal_get_rtc_time,
  203. .set_time = opal_set_rtc_time,
  204. };
  205. static int opal_rtc_probe(struct platform_device *pdev)
  206. {
  207. struct rtc_device *rtc;
  208. if (pdev->dev.of_node &&
  209. (of_property_read_bool(pdev->dev.of_node, "wakeup-source") ||
  210. of_property_read_bool(pdev->dev.of_node, "has-tpo")/* legacy */)) {
  211. device_set_wakeup_capable(&pdev->dev, true);
  212. opal_rtc_ops.read_alarm = opal_get_tpo_time;
  213. opal_rtc_ops.set_alarm = opal_set_tpo_time;
  214. opal_rtc_ops.alarm_irq_enable = opal_tpo_alarm_irq_enable;
  215. }
  216. rtc = devm_rtc_device_register(&pdev->dev, DRVNAME, &opal_rtc_ops,
  217. THIS_MODULE);
  218. if (IS_ERR(rtc))
  219. return PTR_ERR(rtc);
  220. rtc->uie_unsupported = 1;
  221. return 0;
  222. }
  223. static const struct of_device_id opal_rtc_match[] = {
  224. {
  225. .compatible = "ibm,opal-rtc",
  226. },
  227. { }
  228. };
  229. MODULE_DEVICE_TABLE(of, opal_rtc_match);
  230. static const struct platform_device_id opal_rtc_driver_ids[] = {
  231. {
  232. .name = "opal-rtc",
  233. },
  234. { }
  235. };
  236. MODULE_DEVICE_TABLE(platform, opal_rtc_driver_ids);
  237. static struct platform_driver opal_rtc_driver = {
  238. .probe = opal_rtc_probe,
  239. .id_table = opal_rtc_driver_ids,
  240. .driver = {
  241. .name = DRVNAME,
  242. .of_match_table = opal_rtc_match,
  243. },
  244. };
  245. static int __init opal_rtc_init(void)
  246. {
  247. if (!firmware_has_feature(FW_FEATURE_OPAL))
  248. return -ENODEV;
  249. return platform_driver_register(&opal_rtc_driver);
  250. }
  251. static void __exit opal_rtc_exit(void)
  252. {
  253. platform_driver_unregister(&opal_rtc_driver);
  254. }
  255. MODULE_AUTHOR("Neelesh Gupta <neelegup@linux.vnet.ibm.com>");
  256. MODULE_DESCRIPTION("IBM OPAL RTC driver");
  257. MODULE_LICENSE("GPL");
  258. module_init(opal_rtc_init);
  259. module_exit(opal_rtc_exit);