rtc.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. /* last time the RTC got updated */
  22. static long last_rtc_update;
  23. /* time for RTC to update itself in ioclks */
  24. static unsigned long mn10300_rtc_update_period;
  25. /*
  26. * read the current RTC time
  27. */
  28. unsigned long __init get_initial_rtc_time(void)
  29. {
  30. struct rtc_time tm;
  31. get_rtc_time(&tm);
  32. return mktime(tm.tm_year, tm.tm_mon, tm.tm_mday,
  33. tm.tm_hour, tm.tm_min, tm.tm_sec);
  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. BCD_TO_BIN(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. BIN_TO_BCD(real_seconds);
  76. BIN_TO_BCD(real_minutes);
  77. }
  78. CMOS_WRITE(real_seconds, RTC_SECONDS);
  79. CMOS_WRITE(real_minutes, RTC_MINUTES);
  80. } else {
  81. printk(KERN_WARNING
  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. void check_rtc_time(void)
  99. {
  100. /* the RTC clock just finished ticking over again this second
  101. * - if we have an externally synchronized Linux clock, then update
  102. * RTC clock accordingly every ~11 minutes. set_rtc_mmss() has to be
  103. * called as close as possible to 500 ms before the new second starts.
  104. */
  105. if ((time_status & STA_UNSYNC) == 0 &&
  106. xtime.tv_sec > last_rtc_update + 660 &&
  107. xtime.tv_nsec / 1000 >= 500000 - ((unsigned) TICK_SIZE) / 2 &&
  108. xtime.tv_nsec / 1000 <= 500000 + ((unsigned) TICK_SIZE) / 2
  109. ) {
  110. if (set_rtc_mmss(xtime.tv_sec) == 0)
  111. last_rtc_update = xtime.tv_sec;
  112. else
  113. /* do it again in 60s */
  114. last_rtc_update = xtime.tv_sec - 600;
  115. }
  116. }
  117. /*
  118. * calibrate the TSC clock against the RTC
  119. */
  120. void __init calibrate_clock(void)
  121. {
  122. unsigned long count0, counth, count1;
  123. unsigned char status;
  124. /* make sure the RTC is running and is set to operate in 24hr mode */
  125. status = RTSRC;
  126. RTCRB |= RTCRB_SET;
  127. RTCRB |= RTCRB_TM_24HR;
  128. RTCRA |= RTCRA_DVR;
  129. RTCRA &= ~RTCRA_DVR;
  130. RTCRB &= ~RTCRB_SET;
  131. /* work out the clock speed by counting clock cycles between ends of
  132. * the RTC update cycle - track the RTC through one complete update
  133. * cycle (1 second)
  134. */
  135. startup_timestamp_counter();
  136. while (!(RTCRA & RTCRA_UIP)) {}
  137. while ((RTCRA & RTCRA_UIP)) {}
  138. count0 = TMTSCBC;
  139. while (!(RTCRA & RTCRA_UIP)) {}
  140. counth = TMTSCBC;
  141. while ((RTCRA & RTCRA_UIP)) {}
  142. count1 = TMTSCBC;
  143. shutdown_timestamp_counter();
  144. MN10300_TSCCLK = count0 - count1; /* the timers count down */
  145. mn10300_rtc_update_period = counth - count1;
  146. MN10300_TSC_PER_HZ = MN10300_TSCCLK / HZ;
  147. }