rtc.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. void read_persistent_clock(struct timespec *ts)
  26. {
  27. struct rtc_time tm;
  28. get_rtc_time(&tm);
  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. ts->tv_nsec = 0;
  32. }
  33. /*
  34. * In order to set the CMOS clock precisely, set_rtc_mmss has to be called 500
  35. * ms after the second nowtime has started, because when nowtime is written
  36. * into the registers of the CMOS clock, it will jump to the next second
  37. * precisely 500 ms later. Check the Motorola MC146818A or Dallas DS12887 data
  38. * sheet for details.
  39. *
  40. * BUG: This routine does not handle hour overflow properly; it just
  41. * sets the minutes. Usually you'll only notice that after reboot!
  42. */
  43. static int set_rtc_mmss(unsigned long nowtime)
  44. {
  45. unsigned char save_control, save_freq_select;
  46. int retval = 0;
  47. int real_seconds, real_minutes, cmos_minutes;
  48. /* gets recalled with irq locally disabled */
  49. spin_lock(&rtc_lock);
  50. save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being
  51. * set */
  52. CMOS_WRITE(save_control | RTC_SET, RTC_CONTROL);
  53. save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset
  54. * prescaler */
  55. CMOS_WRITE(save_freq_select | RTC_DIV_RESET2, RTC_FREQ_SELECT);
  56. cmos_minutes = CMOS_READ(RTC_MINUTES);
  57. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
  58. cmos_minutes = bcd2bin(cmos_minutes);
  59. /*
  60. * since we're only adjusting minutes and seconds,
  61. * don't interfere with hour overflow. This avoids
  62. * messing with unknown time zones but requires your
  63. * RTC not to be off by more than 15 minutes
  64. */
  65. real_seconds = nowtime % 60;
  66. real_minutes = nowtime / 60;
  67. if (((abs(real_minutes - cmos_minutes) + 15) / 30) & 1)
  68. /* correct for half hour time zone */
  69. real_minutes += 30;
  70. real_minutes %= 60;
  71. if (abs(real_minutes - cmos_minutes) < 30) {
  72. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  73. real_seconds = bin2bcd(real_seconds);
  74. real_minutes = bin2bcd(real_minutes);
  75. }
  76. CMOS_WRITE(real_seconds, RTC_SECONDS);
  77. CMOS_WRITE(real_minutes, RTC_MINUTES);
  78. } else {
  79. printk(KERN_WARNING
  80. "set_rtc_mmss: can't update from %d to %d\n",
  81. cmos_minutes, real_minutes);
  82. retval = -1;
  83. }
  84. /* The following flags have to be released exactly in this order,
  85. * otherwise the DS12887 (popular MC146818A clone with integrated
  86. * battery and quartz) will not reset the oscillator and will not
  87. * update precisely 500 ms later. You won't find this mentioned in
  88. * the Dallas Semiconductor data sheets, but who believes data
  89. * sheets anyway ... -- Markus Kuhn
  90. */
  91. CMOS_WRITE(save_control, RTC_CONTROL);
  92. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  93. spin_unlock(&rtc_lock);
  94. return retval;
  95. }
  96. int update_persistent_clock(struct timespec now)
  97. {
  98. return set_rtc_mms(now.tv_sec);
  99. }
  100. /*
  101. * calibrate the TSC clock against the RTC
  102. */
  103. void __init calibrate_clock(void)
  104. {
  105. unsigned long count0, counth, count1;
  106. unsigned char status;
  107. /* make sure the RTC is running and is set to operate in 24hr mode */
  108. status = RTSRC;
  109. RTCRB |= RTCRB_SET;
  110. RTCRB |= RTCRB_TM_24HR;
  111. RTCRA |= RTCRA_DVR;
  112. RTCRA &= ~RTCRA_DVR;
  113. RTCRB &= ~RTCRB_SET;
  114. /* work out the clock speed by counting clock cycles between ends of
  115. * the RTC update cycle - track the RTC through one complete update
  116. * cycle (1 second)
  117. */
  118. startup_timestamp_counter();
  119. while (!(RTCRA & RTCRA_UIP)) {}
  120. while ((RTCRA & RTCRA_UIP)) {}
  121. count0 = TMTSCBC;
  122. while (!(RTCRA & RTCRA_UIP)) {}
  123. counth = TMTSCBC;
  124. while ((RTCRA & RTCRA_UIP)) {}
  125. count1 = TMTSCBC;
  126. shutdown_timestamp_counter();
  127. MN10300_TSCCLK = count0 - count1; /* the timers count down */
  128. mn10300_rtc_update_period = counth - count1;
  129. MN10300_TSC_PER_HZ = MN10300_TSCCLK / HZ;
  130. }