rtc.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/arch/alpha/kernel/rtc.c
  4. *
  5. * Copyright (C) 1991, 1992, 1995, 1999, 2000 Linus Torvalds
  6. *
  7. * This file contains date handling.
  8. */
  9. #include <linux/errno.h>
  10. #include <linux/init.h>
  11. #include <linux/kernel.h>
  12. #include <linux/param.h>
  13. #include <linux/string.h>
  14. #include <linux/mc146818rtc.h>
  15. #include <linux/bcd.h>
  16. #include <linux/rtc.h>
  17. #include <linux/platform_device.h>
  18. #include "proto.h"
  19. /*
  20. * Support for the RTC device.
  21. *
  22. * We don't want to use the rtc-cmos driver, because we don't want to support
  23. * alarms, as that would be indistinguishable from timer interrupts.
  24. *
  25. * Further, generic code is really, really tied to a 1900 epoch. This is
  26. * true in __get_rtc_time as well as the users of struct rtc_time e.g.
  27. * rtc_tm_to_time. Thankfully all of the other epochs in use are later
  28. * than 1900, and so it's easy to adjust.
  29. */
  30. static unsigned long rtc_epoch;
  31. static int __init
  32. specifiy_epoch(char *str)
  33. {
  34. unsigned long epoch = simple_strtoul(str, NULL, 0);
  35. if (epoch < 1900)
  36. printk("Ignoring invalid user specified epoch %lu\n", epoch);
  37. else
  38. rtc_epoch = epoch;
  39. return 1;
  40. }
  41. __setup("epoch=", specifiy_epoch);
  42. static void __init
  43. init_rtc_epoch(void)
  44. {
  45. int epoch, year, ctrl;
  46. if (rtc_epoch != 0) {
  47. /* The epoch was specified on the command-line. */
  48. return;
  49. }
  50. /* Detect the epoch in use on this computer. */
  51. ctrl = CMOS_READ(RTC_CONTROL);
  52. year = CMOS_READ(RTC_YEAR);
  53. if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
  54. year = bcd2bin(year);
  55. /* PC-like is standard; used for year >= 70 */
  56. epoch = 1900;
  57. if (year < 20) {
  58. epoch = 2000;
  59. } else if (year >= 20 && year < 48) {
  60. /* NT epoch */
  61. epoch = 1980;
  62. } else if (year >= 48 && year < 70) {
  63. /* Digital UNIX epoch */
  64. epoch = 1952;
  65. }
  66. rtc_epoch = epoch;
  67. printk(KERN_INFO "Using epoch %d for rtc year %d\n", epoch, year);
  68. }
  69. static int
  70. alpha_rtc_read_time(struct device *dev, struct rtc_time *tm)
  71. {
  72. int ret = mc146818_get_time(tm);
  73. if (ret < 0) {
  74. dev_err_ratelimited(dev, "unable to read current time\n");
  75. return ret;
  76. }
  77. /* Adjust for non-default epochs. It's easier to depend on the
  78. generic __get_rtc_time and adjust the epoch here than create
  79. a copy of __get_rtc_time with the edits we need. */
  80. if (rtc_epoch != 1900) {
  81. int year = tm->tm_year;
  82. /* Undo the century adjustment made in __get_rtc_time. */
  83. if (year >= 100)
  84. year -= 100;
  85. year += rtc_epoch - 1900;
  86. /* Redo the century adjustment with the epoch in place. */
  87. if (year <= 69)
  88. year += 100;
  89. tm->tm_year = year;
  90. }
  91. return 0;
  92. }
  93. static int
  94. alpha_rtc_set_time(struct device *dev, struct rtc_time *tm)
  95. {
  96. struct rtc_time xtm;
  97. if (rtc_epoch != 1900) {
  98. xtm = *tm;
  99. xtm.tm_year -= rtc_epoch - 1900;
  100. tm = &xtm;
  101. }
  102. return mc146818_set_time(tm);
  103. }
  104. static int
  105. alpha_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
  106. {
  107. switch (cmd) {
  108. case RTC_EPOCH_READ:
  109. return put_user(rtc_epoch, (unsigned long __user *)arg);
  110. case RTC_EPOCH_SET:
  111. if (arg < 1900)
  112. return -EINVAL;
  113. rtc_epoch = arg;
  114. return 0;
  115. default:
  116. return -ENOIOCTLCMD;
  117. }
  118. }
  119. static const struct rtc_class_ops alpha_rtc_ops = {
  120. .read_time = alpha_rtc_read_time,
  121. .set_time = alpha_rtc_set_time,
  122. .ioctl = alpha_rtc_ioctl,
  123. };
  124. /*
  125. * Similarly, except do the actual CMOS access on the boot cpu only.
  126. * This requires marshalling the data across an interprocessor call.
  127. */
  128. #if defined(CONFIG_SMP) && \
  129. (defined(CONFIG_ALPHA_GENERIC) || defined(CONFIG_ALPHA_MARVEL))
  130. # define HAVE_REMOTE_RTC 1
  131. union remote_data {
  132. struct rtc_time *tm;
  133. long retval;
  134. };
  135. static void
  136. do_remote_read(void *data)
  137. {
  138. union remote_data *x = data;
  139. x->retval = alpha_rtc_read_time(NULL, x->tm);
  140. }
  141. static int
  142. remote_read_time(struct device *dev, struct rtc_time *tm)
  143. {
  144. union remote_data x;
  145. if (smp_processor_id() != boot_cpuid) {
  146. x.tm = tm;
  147. smp_call_function_single(boot_cpuid, do_remote_read, &x, 1);
  148. return x.retval;
  149. }
  150. return alpha_rtc_read_time(NULL, tm);
  151. }
  152. static void
  153. do_remote_set(void *data)
  154. {
  155. union remote_data *x = data;
  156. x->retval = alpha_rtc_set_time(NULL, x->tm);
  157. }
  158. static int
  159. remote_set_time(struct device *dev, struct rtc_time *tm)
  160. {
  161. union remote_data x;
  162. if (smp_processor_id() != boot_cpuid) {
  163. x.tm = tm;
  164. smp_call_function_single(boot_cpuid, do_remote_set, &x, 1);
  165. return x.retval;
  166. }
  167. return alpha_rtc_set_time(NULL, tm);
  168. }
  169. static const struct rtc_class_ops remote_rtc_ops = {
  170. .read_time = remote_read_time,
  171. .set_time = remote_set_time,
  172. .ioctl = alpha_rtc_ioctl,
  173. };
  174. #endif
  175. static int __init
  176. alpha_rtc_init(void)
  177. {
  178. struct platform_device *pdev;
  179. struct rtc_device *rtc;
  180. init_rtc_epoch();
  181. pdev = platform_device_register_simple("rtc-alpha", -1, NULL, 0);
  182. rtc = devm_rtc_allocate_device(&pdev->dev);
  183. if (IS_ERR(rtc))
  184. return PTR_ERR(rtc);
  185. platform_set_drvdata(pdev, rtc);
  186. rtc->ops = &alpha_rtc_ops;
  187. #ifdef HAVE_REMOTE_RTC
  188. if (alpha_mv.rtc_boot_cpu_only)
  189. rtc->ops = &remote_rtc_ops;
  190. #endif
  191. return devm_rtc_register_device(rtc);
  192. }
  193. device_initcall(alpha_rtc_init);