rtc-efi.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * rtc-efi: RTC Class Driver for EFI-based systems
  3. *
  4. * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
  5. *
  6. * Author: dann frazier <dannf@hp.com>
  7. * Based on efirtc.c by Stephane Eranian
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. *
  14. */
  15. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/stringify.h>
  19. #include <linux/time.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/rtc.h>
  22. #include <linux/efi.h>
  23. #define EFI_ISDST (EFI_TIME_ADJUST_DAYLIGHT|EFI_TIME_IN_DAYLIGHT)
  24. /*
  25. * EFI Epoch is 1/1/1998
  26. */
  27. #define EFI_RTC_EPOCH 1998
  28. /*
  29. * returns day of the year [0-365]
  30. */
  31. static inline int
  32. compute_yday(efi_time_t *eft)
  33. {
  34. /* efi_time_t.month is in the [1-12] so, we need -1 */
  35. return rtc_year_days(eft->day, eft->month - 1, eft->year);
  36. }
  37. /*
  38. * returns day of the week [0-6] 0=Sunday
  39. *
  40. * Don't try to provide a year that's before 1998, please !
  41. */
  42. static int
  43. compute_wday(efi_time_t *eft)
  44. {
  45. int y;
  46. int ndays = 0;
  47. if (eft->year < EFI_RTC_EPOCH) {
  48. pr_err("EFI year < " __stringify(EFI_RTC_EPOCH) ", invalid date\n");
  49. return -1;
  50. }
  51. for (y = EFI_RTC_EPOCH; y < eft->year; y++)
  52. ndays += 365 + (is_leap_year(y) ? 1 : 0);
  53. ndays += compute_yday(eft);
  54. /*
  55. * 4=1/1/1998 was a Thursday
  56. */
  57. return (ndays + 4) % 7;
  58. }
  59. static void
  60. convert_to_efi_time(struct rtc_time *wtime, efi_time_t *eft)
  61. {
  62. eft->year = wtime->tm_year + 1900;
  63. eft->month = wtime->tm_mon + 1;
  64. eft->day = wtime->tm_mday;
  65. eft->hour = wtime->tm_hour;
  66. eft->minute = wtime->tm_min;
  67. eft->second = wtime->tm_sec;
  68. eft->nanosecond = 0;
  69. eft->daylight = wtime->tm_isdst ? EFI_ISDST : 0;
  70. eft->timezone = EFI_UNSPECIFIED_TIMEZONE;
  71. }
  72. static bool
  73. convert_from_efi_time(efi_time_t *eft, struct rtc_time *wtime)
  74. {
  75. memset(wtime, 0, sizeof(*wtime));
  76. if (eft->second >= 60)
  77. return false;
  78. wtime->tm_sec = eft->second;
  79. if (eft->minute >= 60)
  80. return false;
  81. wtime->tm_min = eft->minute;
  82. if (eft->hour >= 24)
  83. return false;
  84. wtime->tm_hour = eft->hour;
  85. if (!eft->day || eft->day > 31)
  86. return false;
  87. wtime->tm_mday = eft->day;
  88. if (!eft->month || eft->month > 12)
  89. return false;
  90. wtime->tm_mon = eft->month - 1;
  91. wtime->tm_year = eft->year - 1900;
  92. /* day of the week [0-6], Sunday=0 */
  93. wtime->tm_wday = compute_wday(eft);
  94. if (wtime->tm_wday < 0)
  95. return false;
  96. /* day in the year [1-365]*/
  97. wtime->tm_yday = compute_yday(eft);
  98. switch (eft->daylight & EFI_ISDST) {
  99. case EFI_ISDST:
  100. wtime->tm_isdst = 1;
  101. break;
  102. case EFI_TIME_ADJUST_DAYLIGHT:
  103. wtime->tm_isdst = 0;
  104. break;
  105. default:
  106. wtime->tm_isdst = -1;
  107. }
  108. return true;
  109. }
  110. static int efi_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
  111. {
  112. efi_time_t eft;
  113. efi_status_t status;
  114. /*
  115. * As of EFI v1.10, this call always returns an unsupported status
  116. */
  117. status = efi.get_wakeup_time((efi_bool_t *)&wkalrm->enabled,
  118. (efi_bool_t *)&wkalrm->pending, &eft);
  119. if (status != EFI_SUCCESS)
  120. return -EINVAL;
  121. if (!convert_from_efi_time(&eft, &wkalrm->time))
  122. return -EIO;
  123. return rtc_valid_tm(&wkalrm->time);
  124. }
  125. static int efi_set_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
  126. {
  127. efi_time_t eft;
  128. efi_status_t status;
  129. convert_to_efi_time(&wkalrm->time, &eft);
  130. /*
  131. * XXX Fixme:
  132. * As of EFI 0.92 with the firmware I have on my
  133. * machine this call does not seem to work quite
  134. * right
  135. *
  136. * As of v1.10, this call always returns an unsupported status
  137. */
  138. status = efi.set_wakeup_time((efi_bool_t)wkalrm->enabled, &eft);
  139. dev_warn(dev, "write status is %d\n", (int)status);
  140. return status == EFI_SUCCESS ? 0 : -EINVAL;
  141. }
  142. static int efi_read_time(struct device *dev, struct rtc_time *tm)
  143. {
  144. efi_status_t status;
  145. efi_time_t eft;
  146. efi_time_cap_t cap;
  147. status = efi.get_time(&eft, &cap);
  148. if (status != EFI_SUCCESS) {
  149. /* should never happen */
  150. dev_err(dev, "can't read time\n");
  151. return -EINVAL;
  152. }
  153. if (!convert_from_efi_time(&eft, tm))
  154. return -EIO;
  155. return rtc_valid_tm(tm);
  156. }
  157. static int efi_set_time(struct device *dev, struct rtc_time *tm)
  158. {
  159. efi_status_t status;
  160. efi_time_t eft;
  161. convert_to_efi_time(tm, &eft);
  162. status = efi.set_time(&eft);
  163. return status == EFI_SUCCESS ? 0 : -EINVAL;
  164. }
  165. static const struct rtc_class_ops efi_rtc_ops = {
  166. .read_time = efi_read_time,
  167. .set_time = efi_set_time,
  168. .read_alarm = efi_read_alarm,
  169. .set_alarm = efi_set_alarm,
  170. };
  171. static int __init efi_rtc_probe(struct platform_device *dev)
  172. {
  173. struct rtc_device *rtc;
  174. rtc = devm_rtc_device_register(&dev->dev, "rtc-efi", &efi_rtc_ops,
  175. THIS_MODULE);
  176. if (IS_ERR(rtc))
  177. return PTR_ERR(rtc);
  178. platform_set_drvdata(dev, rtc);
  179. return 0;
  180. }
  181. static struct platform_driver efi_rtc_driver = {
  182. .driver = {
  183. .name = "rtc-efi",
  184. .owner = THIS_MODULE,
  185. },
  186. };
  187. module_platform_driver_probe(efi_rtc_driver, efi_rtc_probe);
  188. MODULE_ALIAS("platform:rtc-efi");
  189. MODULE_AUTHOR("dann frazier <dannf@hp.com>");
  190. MODULE_LICENSE("GPL");
  191. MODULE_DESCRIPTION("EFI RTC driver");
  192. MODULE_ALIAS("platform:rtc-efi");