rtc.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. mc146818_get_time(tm);
  73. /* Adjust for non-default epochs. It's easier to depend on the
  74. generic __get_rtc_time and adjust the epoch here than create
  75. a copy of __get_rtc_time with the edits we need. */
  76. if (rtc_epoch != 1900) {
  77. int year = tm->tm_year;
  78. /* Undo the century adjustment made in __get_rtc_time. */
  79. if (year >= 100)
  80. year -= 100;
  81. year += rtc_epoch - 1900;
  82. /* Redo the century adjustment with the epoch in place. */
  83. if (year <= 69)
  84. year += 100;
  85. tm->tm_year = year;
  86. }
  87. return 0;
  88. }
  89. static int
  90. alpha_rtc_set_time(struct device *dev, struct rtc_time *tm)
  91. {
  92. struct rtc_time xtm;
  93. if (rtc_epoch != 1900) {
  94. xtm = *tm;
  95. xtm.tm_year -= rtc_epoch - 1900;
  96. tm = &xtm;
  97. }
  98. return mc146818_set_time(tm);
  99. }
  100. static int
  101. alpha_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
  102. {
  103. switch (cmd) {
  104. case RTC_EPOCH_READ:
  105. return put_user(rtc_epoch, (unsigned long __user *)arg);
  106. case RTC_EPOCH_SET:
  107. if (arg < 1900)
  108. return -EINVAL;
  109. rtc_epoch = arg;
  110. return 0;
  111. default:
  112. return -ENOIOCTLCMD;
  113. }
  114. }
  115. static const struct rtc_class_ops alpha_rtc_ops = {
  116. .read_time = alpha_rtc_read_time,
  117. .set_time = alpha_rtc_set_time,
  118. .ioctl = alpha_rtc_ioctl,
  119. };
  120. /*
  121. * Similarly, except do the actual CMOS access on the boot cpu only.
  122. * This requires marshalling the data across an interprocessor call.
  123. */
  124. #if defined(CONFIG_SMP) && \
  125. (defined(CONFIG_ALPHA_GENERIC) || defined(CONFIG_ALPHA_MARVEL))
  126. # define HAVE_REMOTE_RTC 1
  127. union remote_data {
  128. struct rtc_time *tm;
  129. long retval;
  130. };
  131. static void
  132. do_remote_read(void *data)
  133. {
  134. union remote_data *x = data;
  135. x->retval = alpha_rtc_read_time(NULL, x->tm);
  136. }
  137. static int
  138. remote_read_time(struct device *dev, struct rtc_time *tm)
  139. {
  140. union remote_data x;
  141. if (smp_processor_id() != boot_cpuid) {
  142. x.tm = tm;
  143. smp_call_function_single(boot_cpuid, do_remote_read, &x, 1);
  144. return x.retval;
  145. }
  146. return alpha_rtc_read_time(NULL, tm);
  147. }
  148. static void
  149. do_remote_set(void *data)
  150. {
  151. union remote_data *x = data;
  152. x->retval = alpha_rtc_set_time(NULL, x->tm);
  153. }
  154. static int
  155. remote_set_time(struct device *dev, struct rtc_time *tm)
  156. {
  157. union remote_data x;
  158. if (smp_processor_id() != boot_cpuid) {
  159. x.tm = tm;
  160. smp_call_function_single(boot_cpuid, do_remote_set, &x, 1);
  161. return x.retval;
  162. }
  163. return alpha_rtc_set_time(NULL, tm);
  164. }
  165. static const struct rtc_class_ops remote_rtc_ops = {
  166. .read_time = remote_read_time,
  167. .set_time = remote_set_time,
  168. .ioctl = alpha_rtc_ioctl,
  169. };
  170. #endif
  171. static int __init
  172. alpha_rtc_init(void)
  173. {
  174. const struct rtc_class_ops *ops;
  175. struct platform_device *pdev;
  176. struct rtc_device *rtc;
  177. const char *name;
  178. init_rtc_epoch();
  179. name = "rtc-alpha";
  180. ops = &alpha_rtc_ops;
  181. #ifdef HAVE_REMOTE_RTC
  182. if (alpha_mv.rtc_boot_cpu_only)
  183. ops = &remote_rtc_ops;
  184. #endif
  185. pdev = platform_device_register_simple(name, -1, NULL, 0);
  186. rtc = devm_rtc_device_register(&pdev->dev, name, ops, THIS_MODULE);
  187. if (IS_ERR(rtc))
  188. return PTR_ERR(rtc);
  189. platform_set_drvdata(pdev, rtc);
  190. return 0;
  191. }
  192. device_initcall(alpha_rtc_init);