rtc-efi.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * rtc-efi: RTC Class Driver for EFI-based systems
  4. *
  5. * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
  6. *
  7. * Author: dann frazier <[email protected]>
  8. * Based on efirtc.c by Stephane Eranian
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/stringify.h>
  14. #include <linux/time.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/rtc.h>
  17. #include <linux/efi.h>
  18. #define EFI_ISDST (EFI_TIME_ADJUST_DAYLIGHT|EFI_TIME_IN_DAYLIGHT)
  19. /*
  20. * returns day of the year [0-365]
  21. */
  22. static inline int
  23. compute_yday(efi_time_t *eft)
  24. {
  25. /* efi_time_t.month is in the [1-12] so, we need -1 */
  26. return rtc_year_days(eft->day, eft->month - 1, eft->year);
  27. }
  28. /*
  29. * returns day of the week [0-6] 0=Sunday
  30. */
  31. static int
  32. compute_wday(efi_time_t *eft, int yday)
  33. {
  34. int ndays = eft->year * (365 % 7)
  35. + (eft->year - 1) / 4
  36. - (eft->year - 1) / 100
  37. + (eft->year - 1) / 400
  38. + yday;
  39. /*
  40. * 1/1/0000 may or may not have been a Sunday (if it ever existed at
  41. * all) but assuming it was makes this calculation work correctly.
  42. */
  43. return ndays % 7;
  44. }
  45. static void
  46. convert_to_efi_time(struct rtc_time *wtime, efi_time_t *eft)
  47. {
  48. eft->year = wtime->tm_year + 1900;
  49. eft->month = wtime->tm_mon + 1;
  50. eft->day = wtime->tm_mday;
  51. eft->hour = wtime->tm_hour;
  52. eft->minute = wtime->tm_min;
  53. eft->second = wtime->tm_sec;
  54. eft->nanosecond = 0;
  55. eft->daylight = wtime->tm_isdst ? EFI_ISDST : 0;
  56. eft->timezone = EFI_UNSPECIFIED_TIMEZONE;
  57. }
  58. static bool
  59. convert_from_efi_time(efi_time_t *eft, struct rtc_time *wtime)
  60. {
  61. memset(wtime, 0, sizeof(*wtime));
  62. if (eft->second >= 60)
  63. return false;
  64. wtime->tm_sec = eft->second;
  65. if (eft->minute >= 60)
  66. return false;
  67. wtime->tm_min = eft->minute;
  68. if (eft->hour >= 24)
  69. return false;
  70. wtime->tm_hour = eft->hour;
  71. if (!eft->day || eft->day > 31)
  72. return false;
  73. wtime->tm_mday = eft->day;
  74. if (!eft->month || eft->month > 12)
  75. return false;
  76. wtime->tm_mon = eft->month - 1;
  77. if (eft->year < 1900 || eft->year > 9999)
  78. return false;
  79. wtime->tm_year = eft->year - 1900;
  80. /* day in the year [1-365]*/
  81. wtime->tm_yday = compute_yday(eft);
  82. /* day of the week [0-6], Sunday=0 */
  83. wtime->tm_wday = compute_wday(eft, wtime->tm_yday);
  84. switch (eft->daylight & EFI_ISDST) {
  85. case EFI_ISDST:
  86. wtime->tm_isdst = 1;
  87. break;
  88. case EFI_TIME_ADJUST_DAYLIGHT:
  89. wtime->tm_isdst = 0;
  90. break;
  91. default:
  92. wtime->tm_isdst = -1;
  93. }
  94. return true;
  95. }
  96. static int efi_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
  97. {
  98. efi_time_t eft;
  99. efi_status_t status;
  100. /*
  101. * As of EFI v1.10, this call always returns an unsupported status
  102. */
  103. status = efi.get_wakeup_time((efi_bool_t *)&wkalrm->enabled,
  104. (efi_bool_t *)&wkalrm->pending, &eft);
  105. if (status != EFI_SUCCESS)
  106. return -EINVAL;
  107. if (!convert_from_efi_time(&eft, &wkalrm->time))
  108. return -EIO;
  109. return rtc_valid_tm(&wkalrm->time);
  110. }
  111. static int efi_set_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
  112. {
  113. efi_time_t eft;
  114. efi_status_t status;
  115. convert_to_efi_time(&wkalrm->time, &eft);
  116. /*
  117. * XXX Fixme:
  118. * As of EFI 0.92 with the firmware I have on my
  119. * machine this call does not seem to work quite
  120. * right
  121. *
  122. * As of v1.10, this call always returns an unsupported status
  123. */
  124. status = efi.set_wakeup_time((efi_bool_t)wkalrm->enabled, &eft);
  125. dev_warn(dev, "write status is %d\n", (int)status);
  126. return status == EFI_SUCCESS ? 0 : -EINVAL;
  127. }
  128. static int efi_read_time(struct device *dev, struct rtc_time *tm)
  129. {
  130. efi_status_t status;
  131. efi_time_t eft;
  132. efi_time_cap_t cap;
  133. status = efi.get_time(&eft, &cap);
  134. if (status != EFI_SUCCESS) {
  135. /* should never happen */
  136. dev_err(dev, "can't read time\n");
  137. return -EINVAL;
  138. }
  139. if (!convert_from_efi_time(&eft, tm))
  140. return -EIO;
  141. return 0;
  142. }
  143. static int efi_set_time(struct device *dev, struct rtc_time *tm)
  144. {
  145. efi_status_t status;
  146. efi_time_t eft;
  147. convert_to_efi_time(tm, &eft);
  148. status = efi.set_time(&eft);
  149. return status == EFI_SUCCESS ? 0 : -EINVAL;
  150. }
  151. static int efi_procfs(struct device *dev, struct seq_file *seq)
  152. {
  153. efi_time_t eft, alm;
  154. efi_time_cap_t cap;
  155. efi_bool_t enabled, pending;
  156. struct rtc_device *rtc = dev_get_drvdata(dev);
  157. memset(&eft, 0, sizeof(eft));
  158. memset(&alm, 0, sizeof(alm));
  159. memset(&cap, 0, sizeof(cap));
  160. efi.get_time(&eft, &cap);
  161. efi.get_wakeup_time(&enabled, &pending, &alm);
  162. seq_printf(seq,
  163. "Time\t\t: %u:%u:%u.%09u\n"
  164. "Date\t\t: %u-%u-%u\n"
  165. "Daylight\t: %u\n",
  166. eft.hour, eft.minute, eft.second, eft.nanosecond,
  167. eft.year, eft.month, eft.day,
  168. eft.daylight);
  169. if (eft.timezone == EFI_UNSPECIFIED_TIMEZONE)
  170. seq_puts(seq, "Timezone\t: unspecified\n");
  171. else
  172. /* XXX fixme: convert to string? */
  173. seq_printf(seq, "Timezone\t: %u\n", eft.timezone);
  174. if (test_bit(RTC_FEATURE_ALARM, rtc->features)) {
  175. seq_printf(seq,
  176. "Alarm Time\t: %u:%u:%u.%09u\n"
  177. "Alarm Date\t: %u-%u-%u\n"
  178. "Alarm Daylight\t: %u\n"
  179. "Enabled\t\t: %s\n"
  180. "Pending\t\t: %s\n",
  181. alm.hour, alm.minute, alm.second, alm.nanosecond,
  182. alm.year, alm.month, alm.day,
  183. alm.daylight,
  184. enabled == 1 ? "yes" : "no",
  185. pending == 1 ? "yes" : "no");
  186. if (eft.timezone == EFI_UNSPECIFIED_TIMEZONE)
  187. seq_puts(seq, "Timezone\t: unspecified\n");
  188. else
  189. /* XXX fixme: convert to string? */
  190. seq_printf(seq, "Timezone\t: %u\n", alm.timezone);
  191. }
  192. /*
  193. * now prints the capabilities
  194. */
  195. seq_printf(seq,
  196. "Resolution\t: %u\n"
  197. "Accuracy\t: %u\n"
  198. "SetstoZero\t: %u\n",
  199. cap.resolution, cap.accuracy, cap.sets_to_zero);
  200. return 0;
  201. }
  202. static const struct rtc_class_ops efi_rtc_ops = {
  203. .read_time = efi_read_time,
  204. .set_time = efi_set_time,
  205. .read_alarm = efi_read_alarm,
  206. .set_alarm = efi_set_alarm,
  207. .proc = efi_procfs,
  208. };
  209. static int __init efi_rtc_probe(struct platform_device *dev)
  210. {
  211. struct rtc_device *rtc;
  212. efi_time_t eft;
  213. efi_time_cap_t cap;
  214. /* First check if the RTC is usable */
  215. if (efi.get_time(&eft, &cap) != EFI_SUCCESS)
  216. return -ENODEV;
  217. rtc = devm_rtc_allocate_device(&dev->dev);
  218. if (IS_ERR(rtc))
  219. return PTR_ERR(rtc);
  220. platform_set_drvdata(dev, rtc);
  221. rtc->ops = &efi_rtc_ops;
  222. clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, rtc->features);
  223. if (efi_rt_services_supported(EFI_RT_SUPPORTED_WAKEUP_SERVICES))
  224. set_bit(RTC_FEATURE_ALARM_WAKEUP_ONLY, rtc->features);
  225. else
  226. clear_bit(RTC_FEATURE_ALARM, rtc->features);
  227. return devm_rtc_register_device(rtc);
  228. }
  229. static struct platform_driver efi_rtc_driver = {
  230. .driver = {
  231. .name = "rtc-efi",
  232. },
  233. };
  234. module_platform_driver_probe(efi_rtc_driver, efi_rtc_probe);
  235. MODULE_AUTHOR("dann frazier <[email protected]>");
  236. MODULE_LICENSE("GPL");
  237. MODULE_DESCRIPTION("EFI RTC driver");
  238. MODULE_ALIAS("platform:rtc-efi");