rtc.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* MN10300 RTC management
  2. *
  3. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/mc146818rtc.h>
  15. #include <linux/bcd.h>
  16. #include <linux/timex.h>
  17. #include <asm/rtc-regs.h>
  18. #include <asm/rtc.h>
  19. DEFINE_SPINLOCK(rtc_lock);
  20. EXPORT_SYMBOL(rtc_lock);
  21. /*
  22. * Read the current RTC time
  23. */
  24. void read_persistent_clock(struct timespec *ts)
  25. {
  26. struct rtc_time tm;
  27. get_rtc_time(&tm);
  28. ts->tv_nsec = 0;
  29. ts->tv_sec = mktime(tm.tm_year, tm.tm_mon, tm.tm_mday,
  30. tm.tm_hour, tm.tm_min, tm.tm_sec);
  31. /* if rtc is way off in the past, set something reasonable */
  32. if (ts->tv_sec < 0)
  33. ts->tv_sec = mktime(2009, 1, 1, 12, 0, 0);
  34. }
  35. /*
  36. * In order to set the CMOS clock precisely, set_rtc_mmss has to be called 500
  37. * ms after the second nowtime has started, because when nowtime is written
  38. * into the registers of the CMOS clock, it will jump to the next second
  39. * precisely 500 ms later. Check the Motorola MC146818A or Dallas DS12887 data
  40. * sheet for details.
  41. *
  42. * BUG: This routine does not handle hour overflow properly; it just
  43. * sets the minutes. Usually you'll only notice that after reboot!
  44. */
  45. static int set_rtc_mmss(unsigned long nowtime)
  46. {
  47. unsigned char save_control, save_freq_select;
  48. int retval = 0;
  49. int real_seconds, real_minutes, cmos_minutes;
  50. /* gets recalled with irq locally disabled */
  51. spin_lock(&rtc_lock);
  52. save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being
  53. * set */
  54. CMOS_WRITE(save_control | RTC_SET, RTC_CONTROL);
  55. save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset
  56. * prescaler */
  57. CMOS_WRITE(save_freq_select | RTC_DIV_RESET2, RTC_FREQ_SELECT);
  58. cmos_minutes = CMOS_READ(RTC_MINUTES);
  59. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
  60. cmos_minutes = bcd2bin(cmos_minutes);
  61. /*
  62. * since we're only adjusting minutes and seconds,
  63. * don't interfere with hour overflow. This avoids
  64. * messing with unknown time zones but requires your
  65. * RTC not to be off by more than 15 minutes
  66. */
  67. real_seconds = nowtime % 60;
  68. real_minutes = nowtime / 60;
  69. if (((abs(real_minutes - cmos_minutes) + 15) / 30) & 1)
  70. /* correct for half hour time zone */
  71. real_minutes += 30;
  72. real_minutes %= 60;
  73. if (abs(real_minutes - cmos_minutes) < 30) {
  74. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  75. real_seconds = bin2bcd(real_seconds);
  76. real_minutes = bin2bcd(real_minutes);
  77. }
  78. CMOS_WRITE(real_seconds, RTC_SECONDS);
  79. CMOS_WRITE(real_minutes, RTC_MINUTES);
  80. } else {
  81. printk_once(KERN_NOTICE
  82. "set_rtc_mmss: can't update from %d to %d\n",
  83. cmos_minutes, real_minutes);
  84. retval = -1;
  85. }
  86. /* The following flags have to be released exactly in this order,
  87. * otherwise the DS12887 (popular MC146818A clone with integrated
  88. * battery and quartz) will not reset the oscillator and will not
  89. * update precisely 500 ms later. You won't find this mentioned in
  90. * the Dallas Semiconductor data sheets, but who believes data
  91. * sheets anyway ... -- Markus Kuhn
  92. */
  93. CMOS_WRITE(save_control, RTC_CONTROL);
  94. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  95. spin_unlock(&rtc_lock);
  96. return retval;
  97. }
  98. int update_persistent_clock(struct timespec now)
  99. {
  100. return set_rtc_mmss(now.tv_sec);
  101. }
  102. /*
  103. * calibrate the TSC clock against the RTC
  104. */
  105. void __init calibrate_clock(void)
  106. {
  107. unsigned char status;
  108. /* make sure the RTC is running and is set to operate in 24hr mode */
  109. status = RTSRC;
  110. RTCRB |= RTCRB_SET;
  111. RTCRB |= RTCRB_TM_24HR;
  112. RTCRB &= ~RTCRB_DM_BINARY;
  113. RTCRA |= RTCRA_DVR;
  114. RTCRA &= ~RTCRA_DVR;
  115. RTCRB &= ~RTCRB_SET;
  116. }