rtc.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * linux/arch/alpha/kernel/rtc.c
  3. *
  4. * Copyright (C) 1991, 1992, 1995, 1999, 2000 Linus Torvalds
  5. *
  6. * This file contains date handling.
  7. */
  8. #include <linux/errno.h>
  9. #include <linux/init.h>
  10. #include <linux/kernel.h>
  11. #include <linux/param.h>
  12. #include <linux/string.h>
  13. #include <linux/mc146818rtc.h>
  14. #include <linux/bcd.h>
  15. #include <linux/rtc.h>
  16. #include <linux/platform_device.h>
  17. #include "proto.h"
  18. /*
  19. * Support for the RTC device.
  20. *
  21. * We don't want to use the rtc-cmos driver, because we don't want to support
  22. * alarms, as that would be indistinguishable from timer interrupts.
  23. *
  24. * Further, generic code is really, really tied to a 1900 epoch. This is
  25. * true in __get_rtc_time as well as the users of struct rtc_time e.g.
  26. * rtc_tm_to_time. Thankfully all of the other epochs in use are later
  27. * than 1900, and so it's easy to adjust.
  28. */
  29. static unsigned long rtc_epoch;
  30. static int __init
  31. specifiy_epoch(char *str)
  32. {
  33. unsigned long epoch = simple_strtoul(str, NULL, 0);
  34. if (epoch < 1900)
  35. printk("Ignoring invalid user specified epoch %lu\n", epoch);
  36. else
  37. rtc_epoch = epoch;
  38. return 1;
  39. }
  40. __setup("epoch=", specifiy_epoch);
  41. static void __init
  42. init_rtc_epoch(void)
  43. {
  44. int epoch, year, ctrl;
  45. if (rtc_epoch != 0) {
  46. /* The epoch was specified on the command-line. */
  47. return;
  48. }
  49. /* Detect the epoch in use on this computer. */
  50. ctrl = CMOS_READ(RTC_CONTROL);
  51. year = CMOS_READ(RTC_YEAR);
  52. if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
  53. year = bcd2bin(year);
  54. /* PC-like is standard; used for year >= 70 */
  55. epoch = 1900;
  56. if (year < 20) {
  57. epoch = 2000;
  58. } else if (year >= 20 && year < 48) {
  59. /* NT epoch */
  60. epoch = 1980;
  61. } else if (year >= 48 && year < 70) {
  62. /* Digital UNIX epoch */
  63. epoch = 1952;
  64. }
  65. rtc_epoch = epoch;
  66. printk(KERN_INFO "Using epoch %d for rtc year %d\n", epoch, year);
  67. }
  68. static int
  69. alpha_rtc_read_time(struct device *dev, struct rtc_time *tm)
  70. {
  71. mc146818_get_time(tm);
  72. /* Adjust for non-default epochs. It's easier to depend on the
  73. generic __get_rtc_time and adjust the epoch here than create
  74. a copy of __get_rtc_time with the edits we need. */
  75. if (rtc_epoch != 1900) {
  76. int year = tm->tm_year;
  77. /* Undo the century adjustment made in __get_rtc_time. */
  78. if (year >= 100)
  79. year -= 100;
  80. year += rtc_epoch - 1900;
  81. /* Redo the century adjustment with the epoch in place. */
  82. if (year <= 69)
  83. year += 100;
  84. tm->tm_year = year;
  85. }
  86. return rtc_valid_tm(tm);
  87. }
  88. static int
  89. alpha_rtc_set_time(struct device *dev, struct rtc_time *tm)
  90. {
  91. struct rtc_time xtm;
  92. if (rtc_epoch != 1900) {
  93. xtm = *tm;
  94. xtm.tm_year -= rtc_epoch - 1900;
  95. tm = &xtm;
  96. }
  97. return mc146818_set_time(tm);
  98. }
  99. static int
  100. alpha_rtc_set_mmss(struct device *dev, time64_t nowtime)
  101. {
  102. int retval = 0;
  103. int real_seconds, real_minutes, cmos_minutes;
  104. unsigned char save_control, save_freq_select;
  105. /* Note: This code only updates minutes and seconds. Comments
  106. indicate this was to avoid messing with unknown time zones,
  107. and with the epoch nonsense described above. In order for
  108. this to work, the existing clock cannot be off by more than
  109. 15 minutes.
  110. ??? This choice is may be out of date. The x86 port does
  111. not have problems with timezones, and the epoch processing has
  112. now been fixed in alpha_set_rtc_time.
  113. In either case, one can always force a full rtc update with
  114. the userland hwclock program, so surely 15 minute accuracy
  115. is no real burden. */
  116. /* In order to set the CMOS clock precisely, we have to be called
  117. 500 ms after the second nowtime has started, because when
  118. nowtime is written into the registers of the CMOS clock, it will
  119. jump to the next second precisely 500 ms later. Check the Motorola
  120. MC146818A or Dallas DS12887 data sheet for details. */
  121. /* irq are locally disabled here */
  122. spin_lock(&rtc_lock);
  123. /* Tell the clock it's being set */
  124. save_control = CMOS_READ(RTC_CONTROL);
  125. CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
  126. /* Stop and reset prescaler */
  127. save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
  128. CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
  129. cmos_minutes = CMOS_READ(RTC_MINUTES);
  130. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
  131. cmos_minutes = bcd2bin(cmos_minutes);
  132. real_seconds = nowtime % 60;
  133. real_minutes = nowtime / 60;
  134. if (((abs(real_minutes - cmos_minutes) + 15) / 30) & 1) {
  135. /* correct for half hour time zone */
  136. real_minutes += 30;
  137. }
  138. real_minutes %= 60;
  139. if (abs(real_minutes - cmos_minutes) < 30) {
  140. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  141. real_seconds = bin2bcd(real_seconds);
  142. real_minutes = bin2bcd(real_minutes);
  143. }
  144. CMOS_WRITE(real_seconds,RTC_SECONDS);
  145. CMOS_WRITE(real_minutes,RTC_MINUTES);
  146. } else {
  147. printk_once(KERN_NOTICE
  148. "set_rtc_mmss: can't update from %d to %d\n",
  149. cmos_minutes, real_minutes);
  150. retval = -1;
  151. }
  152. /* The following flags have to be released exactly in this order,
  153. * otherwise the DS12887 (popular MC146818A clone with integrated
  154. * battery and quartz) will not reset the oscillator and will not
  155. * update precisely 500 ms later. You won't find this mentioned in
  156. * the Dallas Semiconductor data sheets, but who believes data
  157. * sheets anyway ... -- Markus Kuhn
  158. */
  159. CMOS_WRITE(save_control, RTC_CONTROL);
  160. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  161. spin_unlock(&rtc_lock);
  162. return retval;
  163. }
  164. static int
  165. alpha_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
  166. {
  167. switch (cmd) {
  168. case RTC_EPOCH_READ:
  169. return put_user(rtc_epoch, (unsigned long __user *)arg);
  170. case RTC_EPOCH_SET:
  171. if (arg < 1900)
  172. return -EINVAL;
  173. rtc_epoch = arg;
  174. return 0;
  175. default:
  176. return -ENOIOCTLCMD;
  177. }
  178. }
  179. static const struct rtc_class_ops alpha_rtc_ops = {
  180. .read_time = alpha_rtc_read_time,
  181. .set_time = alpha_rtc_set_time,
  182. .set_mmss64 = alpha_rtc_set_mmss,
  183. .ioctl = alpha_rtc_ioctl,
  184. };
  185. /*
  186. * Similarly, except do the actual CMOS access on the boot cpu only.
  187. * This requires marshalling the data across an interprocessor call.
  188. */
  189. #if defined(CONFIG_SMP) && \
  190. (defined(CONFIG_ALPHA_GENERIC) || defined(CONFIG_ALPHA_MARVEL))
  191. # define HAVE_REMOTE_RTC 1
  192. union remote_data {
  193. struct rtc_time *tm;
  194. unsigned long now;
  195. long retval;
  196. };
  197. static void
  198. do_remote_read(void *data)
  199. {
  200. union remote_data *x = data;
  201. x->retval = alpha_rtc_read_time(NULL, x->tm);
  202. }
  203. static int
  204. remote_read_time(struct device *dev, struct rtc_time *tm)
  205. {
  206. union remote_data x;
  207. if (smp_processor_id() != boot_cpuid) {
  208. x.tm = tm;
  209. smp_call_function_single(boot_cpuid, do_remote_read, &x, 1);
  210. return x.retval;
  211. }
  212. return alpha_rtc_read_time(NULL, tm);
  213. }
  214. static void
  215. do_remote_set(void *data)
  216. {
  217. union remote_data *x = data;
  218. x->retval = alpha_rtc_set_time(NULL, x->tm);
  219. }
  220. static int
  221. remote_set_time(struct device *dev, struct rtc_time *tm)
  222. {
  223. union remote_data x;
  224. if (smp_processor_id() != boot_cpuid) {
  225. x.tm = tm;
  226. smp_call_function_single(boot_cpuid, do_remote_set, &x, 1);
  227. return x.retval;
  228. }
  229. return alpha_rtc_set_time(NULL, tm);
  230. }
  231. static void
  232. do_remote_mmss(void *data)
  233. {
  234. union remote_data *x = data;
  235. x->retval = alpha_rtc_set_mmss(NULL, x->now);
  236. }
  237. static int
  238. remote_set_mmss(struct device *dev, time64_t now)
  239. {
  240. union remote_data x;
  241. if (smp_processor_id() != boot_cpuid) {
  242. x.now = now;
  243. smp_call_function_single(boot_cpuid, do_remote_mmss, &x, 1);
  244. return x.retval;
  245. }
  246. return alpha_rtc_set_mmss(NULL, now);
  247. }
  248. static const struct rtc_class_ops remote_rtc_ops = {
  249. .read_time = remote_read_time,
  250. .set_time = remote_set_time,
  251. .set_mmss64 = remote_set_mmss,
  252. .ioctl = alpha_rtc_ioctl,
  253. };
  254. #endif
  255. static int __init
  256. alpha_rtc_init(void)
  257. {
  258. const struct rtc_class_ops *ops;
  259. struct platform_device *pdev;
  260. struct rtc_device *rtc;
  261. const char *name;
  262. init_rtc_epoch();
  263. name = "rtc-alpha";
  264. ops = &alpha_rtc_ops;
  265. #ifdef HAVE_REMOTE_RTC
  266. if (alpha_mv.rtc_boot_cpu_only)
  267. ops = &remote_rtc_ops;
  268. #endif
  269. pdev = platform_device_register_simple(name, -1, NULL, 0);
  270. rtc = devm_rtc_device_register(&pdev->dev, name, ops, THIS_MODULE);
  271. if (IS_ERR(rtc))
  272. return PTR_ERR(rtc);
  273. platform_set_drvdata(pdev, rtc);
  274. return 0;
  275. }
  276. device_initcall(alpha_rtc_init);